在 promise 中更直接地使用函数

Using a function more directly in a promise

有没有办法表达这个:

    imagePFFileFromUrl(exampUrl) // (returns a promise)
    .then(function(xxx))
        // that syntax gives you the results of the
        // imagePFFileFromUrl inside the variable xxx.
        // I then use it in another function which
        // returns another promise...
        {
        return saveImageFile(xxx, anotherArgument) // (returns a promise)
        })
    .then(function(savedPFFile)

更多类似的..

    imagePFFileFromUrl(exampUrl)
    .then(saveImageFile(...the results from imagePFFileFromUrl ...))
    .then(function(savedPFFile)

什么时候使用 promise?

您的 .then() 需要有 1 或 2 个回调函数。如果有 1,它会接受承诺的已解决值。如果有两个,则第二个采用承诺的拒绝值。所以,不,你不能做你想做的事。


这是一个很好的 模式,有两个参数,请注意 return synchUsers

您可以使用函数bind来实现。 Bind 创建一个新函数,第一个参数是 this 对象,后面的任何参数都传递给该函数。当调用由 bind() 创建的新函数时,除了传递给 bind() 的参数之外的任何参数。

imagePFFileFromUrl(exampUrl)
  .then(saveImageFile.bind(null, "name"))
  .then(function(savedPFFile)

所以本质上,您将第一个承诺的 return 值作为第二个参数传递给 saveImageFile 函数。第一个参数是字符串值 "name".