相关疑难解决方法(0)

Nodejs Promise:如何在 Promise.all 函数中连接已解析的数据

更新:我自己找到了一个“自制”解决方案,向下滚动到这个解决方案!

当我“手动”执行 NodeJS Promise 时,我可以选择将解析值从父 Promise 传递给它的子 Promise,或者更准确地说,从第一个 Promise 传递给第二个 Promise,依此类推。

因此,在第一个承诺完成后,我可以在下一个承诺中继续使用它已解析的数据。有时这些数据对于整个过程至关重要。例如通过远程API发送短信,如果短信不发送,我将无法管理下一个功能。

我尝试过“快捷方式”(*抱歉,如果它不是快捷方式):Promise.all。嗯,这很好,它首先弹出捕获的承诺拒绝值,如果一切正常,它就会完成。但是我怎样才能以上面描述的相同方式保存解析的数据呢?

我将在这里推送一些代码,以便我们可以更好地相互理解:

尝试使用 Promise.all 来做到这一点

function test1() {
    return new Promise(function(resolve, reject) {
      resolve("test1");
    });
  }

  function test2() {
    return new Promise(function(resolve, reject) {
      resolve("test2");
    });
  }

  function test3() {
    return new Promise(function(resolve, reject) {
      resolve("test3");
    });
  }

  Promise.all([test1, test2, test3].map(func => func())).then((result) => {
    console.log('result: ', result);
  }).catch((result) => {
    console.log("Catched: ", result);
  });
Run Code Online (Sandbox Code Playgroud)

尝试使用“手动”方式执行此操作:

function test1() {
    return new Promise(function(resolve, reject) {
      resolve("test1");
    });
  }

  function …
Run Code Online (Sandbox Code Playgroud)

javascript node.js angularjs

2
推荐指数
1
解决办法
1万
查看次数

当函数需要访问外部范围变量时,更好的方法来处理Promise流

我正在创建一个Node.js模块来与我的API进行交互,我使用superagent模块来执行请求.这个怎么运作:

module.exports = data => {
  return getUploadUrl()
    .then(uploadFiles)
    .then(initializeSwam)

  function getUploadUrl() {
    const request = superagent.get(....)
    return request
  }

  function uploadFiles(responseFromGetUploadUrl) {
    const request = superagent.post(responseFromGetUploadUrl.body.url)
    // attach files that are in data.files
    return request
  }

  function initializeSwam(responseFromUploadFilesRequest) {
    // Same thing here. I need access data and repsonseFromUploadFilesRequest.body
  }
}
Run Code Online (Sandbox Code Playgroud)

我觉得我做错了,但我想不出更好的方法来达到同样的效果.

javascript node.js promise

2
推荐指数
1
解决办法
44
查看次数

如何避免承诺链钻探?

我的承诺链如下所示:

PromiseA()
.then((A) => PromiseB(A))
.then((B) => PromiseC(B))
...
.then((X) => PromiseY(X))
.then((Y) => PromiseZ(Y, A))
Run Code Online (Sandbox Code Playgroud)

如何在最后一个承诺中使用参数 A 而无需钻取所有承诺,如下所示:

PromiseA()
.then((A) => Promise.all[A, PromiseB(A)])
.then(([A, B]) => Promise.all[A, PromiseC(B)])
...
.then(([A, X]) => Promise.all[A, PromiseY(X)])
.then(([A, Y]) => PromiseZ(A, Y))
Run Code Online (Sandbox Code Playgroud)

javascript promise

2
推荐指数
1
解决办法
80
查看次数

如何从then()函数中的Promise访问内容,在next then()函数中访问它

如何从then()函数中的Promise访问内容,在next then()函数中访问它.

我的问题大致通过以下代码解释.

someRandomPromiseFunction().then(function(theArray) {
  var newProm = _.map(theArray, function(arrayItem) {
    return new Promise(function(resolve, reject) {
      resolve(arrayItem);
    });
  }
  Promise.all(newProm).then(function(theArray) {
    return theArray; // How do I access this in the next then() function
  }).catch(function(err) {
    return err;
  });
}).then(function(theArray) {
  console.log(theArray); // I need to access theArray from the Promise.all HERE
});
Run Code Online (Sandbox Code Playgroud)

javascript resolve promise access

1
推荐指数
1
解决办法
82
查看次数

标签 统计

javascript ×4

promise ×3

node.js ×2

access ×1

angularjs ×1

resolve ×1