1

我需要链接使用请求承诺的承诺,所以它有点链接嵌套的承诺。

想象一下代码:

const rp = require('request-promise');

function doStuff(){
  for ( let i = 0; i <= 10; i++ ){
    methodA();
  }
};

function methodA(){
  let options = {...};
  rp(options)
    .then(result => methodB(result))
    .catch(err => console.log(err));
};

function methodB(resultA){
  let options = {uri: resultA};
  rp(options)
    .then(result => methodC(resultA, result))
    .catch(err => console.log(err));
};

function methodC(resultA, resultB){
  //some calculations
};

在 doStuff 中,我需要等待方法 C 的所有十次执行的结果并将它们收集到数组中。我试图像这样链接它:

function doStuff(){
  for ( let i = 0; i <= 10; i++ ){
    let promiseA = new Promise((resolve, reject) => {
      resolve(methodA());
    });
    let promiseB = promiseA.then(result => methodB(result));
    let promiseC = promiseB.then(result => methodC(promiseA.result, result));
    Promise.all([promiseA, promiseB, promiseC]);
  }
};

但可以肯定的是,它不会起作用,因为在 methodA 和 methodB 中我们有异步的 HTTP 请求。因此,promiseB 中的结果是未定义的。

这意味着,问题是:如果它们有嵌套的承诺,如何链接承诺?(以及最终如何收集结果?)

谢谢!

更新:链接承诺也没有太大帮助,因为 1 返回 AB 的先前数组,但反之亦然:

function methodA(){
    let promise = new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log('Resolved A');
            resolve('A');
        }, Math.random() * 2000);
    });

    return promise
        .then(result => methodB(result))
        .catch(err => console.log(err));
}

function methodB(resultA){
    let promise = new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log('Resolved B');
            resolve('B');
        }, Math.random() * 2000);
    });

    return promise
        .then(result => methodC(resultA, result))
        .catch(err => console.log(err));
}

function methodC(resultA, resultB){
    return resultA + resultB;
}

function doStuff() {
    let promises = [];

    for (let i = 0; i <= 10; i++){
        promises.push(methodA());
    }

    Promise.all(promises).then(results => {
        console.log(results);
    });
    return 1;
}

console.log(doStuff());
4

3 回答 3

2

您的每个函数都需要返回它们的承诺:

function methodA(){
    let options = {...};
    return rp(options)
        .then(result => methodB(result))
        .catch(err => console.log(err));
}

function methodB(resultA){
    let options = {uri: resultA};
    return rp(options)
        .then(result => methodC(resultA, result))
        .catch(err => console.log(err));
}

function methodC(resultA, resultB){
    //some calculations
}

function doStuff() {
    let promises = [];
    for ( let i = 0; i <= 10; i++ ){
        promises.push(methodA());
    }
    Promise.all(promises).then(...)
}

编辑:我创建了一个测试示例,它在methodAmethodB. 每个 Promise 都会持续 0 到 2 秒。它似乎正在工作:

function methodA(){
    let promise = new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log('Resolved A');
            resolve('A');
        }, Math.random() * 2000);
    });

    return promise
        .then(result => methodB(result))
        .catch(err => console.log(err));
}

function methodB(resultA){
    let promise = new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log('Resolved B');
            resolve('B');
        }, Math.random() * 2000);
    });

    return promise
        .then(result => methodC(resultA, result))
        .catch(err => console.log(err));
}

function methodC(resultA, resultB){
    return resultA + resultB;
}

function doStuff() {
    let promises = [];

    for (let i = 0; i <= 10; i++){
        promises.push(methodA());
    }

    return Promise.all(promises).then(results => {
        console.log(results);

        return 1;
    });
}

doStuff().then(result => {
    console.log(result);
});
于 2017-05-24T13:46:39.747 回答
0

@Frank_Modica 的回答是要走的路。

只想补充一点,如果您启用async/await,您可以这样做。但它确实需要Babelor Typescript

async function myMethod() {
    const options = { }
    try {
        const result_a = await rp(options)
        const result_b = await rp({ uri: result_a })
        const result_c = ...
    } catch(err) {
        console.log(err)
    }
}

for (let i = 0; i <= 10; i++) {
    await myMethod();
}
于 2017-05-24T13:39:22.300 回答
-1

它必须轻轻更改为:

Promise.all([promiseA, promiseB, promiseC]).then([promiseD]);

同样在函数本身中,它必须是一个返回语句才能使它们链接起来。对于请求本身,只需添加:{async: false}

它也可以用于:

var runSequence = require('run-sequence');
function methodA () {
    return runSequence(
        'promiseA',
        'promiseB',
        ['promiseC1', 'promiseC2'],
        'promiseD',
        callback
    );
}
于 2017-05-24T13:41:51.990 回答