顺序承诺问题

时间:2018-10-04 06:43:55

标签: javascript es6-promise

我试图正确地理解诺言。

我有一个睡眠功能,如果我经过一段时间,它会延迟执行它。我想执行Sequence中的所有内容。

问题:

1: 但是,有时甚至在诺言未兑现之前,我的工作就会被召唤。有人可以告诉我代码中的错误在哪里吗?

2:我可以在这里使用异步等待而不是承诺吗?如果是,怎么办?

我的尝试

/* Do the following: 
2- creating sequential promise, by wait for 1s, 2s and 3s sequentially. And showing result for each promise is done.
3- creating parallel promise, by wait for 1s, 2s and 3s parallelly. And showing result when all promise is done.
4- creating mixed promise, by wait 1s, 2s squentially, after then wait for 3s, 4s, 5s parallelly.
5- createing sequential promise, by wait for 1s, undefined and 3s sequentially. catch the error.
6- createing sequential promise, by wait for 5 different numbers, with random number as input.
*/

// Sleep takes time and returns time+1 
const sleep = (time) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log(`Resolving: ${time}+1`);
            resolve(time+1);
        }, time*1000);
    });
}


// Generate Random number within a range.
const randomNumber = (min=1, max=8) => { 
    return Math.floor(Math.random() * (max-min+1) + min);
 } // -5~5
 

console.log(
sleep(randomNumber())
    .then(sleep(randomNumber()))
    .then(sleep(randomNumber()))
    .then(sleep(randomNumber()))
    .then(sleep(randomNumber()))
    .then((e)=> {
        console.log("Done", e); // Done called when all promises are done.
    })
)

1 个答案:

答案 0 :(得分:1)

.then接受一个 function 参数,而不是Promise。更改您的

.then(sleep(randomNumber()))

.then(() => sleep(randomNumber)

,以便将功能放入Promise链中,当前一个.then解析时将调用该链。否则,一旦创建Promise链,每个sleep(randomNumber())就会被立即调用

/* Do the following: 
2- creating sequential promise, by wait for 1s, 2s and 3s sequentially. And showing result for each promise is done.
3- creating parallel promise, by wait for 1s, 2s and 3s parallelly. And showing result when all promise is done.
4- creating mixed promise, by wait 1s, 2s squentially, after then wait for 3s, 4s, 5s parallelly.
5- createing sequential promise, by wait for 1s, undefined and 3s sequentially. catch the error.
6- createing sequential promise, by wait for 5 different numbers, with random number as input.
*/

// Sleep takes time and returns time+1 
const sleep = (time) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log(`Resolving: ${time}+1`);
            resolve(time+1);
        }, time*1000);
    });
}


// Generate Random number within a range.
const randomeNumber = (min=1, max=8) => { 
    return Math.floor(Math.random() * (max-min+1) + min);
 } // -5~5
 

console.log(
sleep(randomeNumber())
    .then(() => sleep(randomeNumber()))
    .then(() => sleep(randomeNumber()))
    .then(() => sleep(randomeNumber()))
    .then(() => sleep(randomeNumber()))
    .then((e)=> {
        console.log("Done", e); // Done called when all promises are done.
    })
)

要并行运行它们并在它们全部解决后运行某些程序,请使用Promise.all

Promise.all([
  sleep(randomeNumber()),
  sleep(randomeNumber()),
  sleep(randomeNumber()),
  sleep(randomeNumber()),
  sleep(randomeNumber())
])
  .then(() => console.log('all done'));