在声明上运行的 Promise 数组

问题描述 投票:0回答:1

看起来 Promise 是在数组中声明后立即运行的,而不是在调用 Promise.all 时运行。

    p1 = new Promise(function (resolve, reject) {
        console.log('running geek1');
        const date = Date.now();
        let currentDate = null;
        do {
            currentDate = Date.now();
        } while (currentDate - date < 2000);
        resolve('timeout geek1');
    });

    p3 = new Promise(function (resolve, reject) {
        console.log('running geek3');
        const date = Date.now();
        let currentDate = null;
        do {
            currentDate = Date.now();
        } while (currentDate - date < 2000);
        resolve('geek3');
    });
    p4 = function () {
        return new Promise(function (resolve, reject) {
            console.log('running geek4');
            resolve('geek4');
        });
    }
    p5 = new Promise(function (resolve, reject) {
        console.log('running geek5');
        resolve('geek5');
    });

当你运行它时,请注意我设置的延迟,它会暂停 javascript 的执行。 这对我来说意味着一旦我声明它,就会运行承诺中的代码。

results are:

running geek1
running geek3
running geek5

注意它没有运行 p4,因为我返回了承诺,所以它不会立即执行。

但是如果我添加

Promise.all([p1, p4, p5, p3]).then(function (values) {
    console.log(values);
});

我得到以下内容

running geek1
running geek3
running geek5
['timeout geek1', [Function: p4], 'geek5', 'geek3']

请注意,p4 并未在 Promise.all 下运行,而是以函数形式返回值。

如何创建动态的 Promise 数组以在 Promise.all 下运行它们?

我发现了一些关于技术的相互矛盾的例子。 为什么我需要返回新的承诺以确保它不会在声明时运行,这是没有意义的。 它是一个构造函数,我传入一个函数定义。 那么为什么它会运行?

如果我做了以下事情:

    class SampleClass {
        constructor(testFunc, value) {
            this.testFunc = testFunc;
            this.value = value;
        }
        writeIt(){
            console.log(this.testFunc(this.value));
        }
    }

    console.log("declare");

    let sc = new SampleClass((value) => { console.log(value); value++; console.log(value); return "added value" }, 453);
    console.log("between");
    sc.writeIt();

它按照我的预期工作,直到我调用 writeIt() 之前,值不会被写入 这就是它返回的内容。

declare
between
453
454
added value

非常感谢您的帮助。

javascript promise ecmascript-5
1个回答
0
投票

首先,不要这样做,因为它会阻塞主线程。

do {
  currentDate = Date.now();
} while (currentDate - date < 2000);

如果需要延迟,请使用

setTimeout

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));

然后你可以创建一个函数来包装

Promise.all
。对于所有
promises
,如果它是一个函数,则运行它并获取其返回值,否则保持原样。

const runAllPromises = promises => Promise.all(
  promises.map(promise => typeof promise === 'function' ? promise() : promise)
);

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));

const p1 = new Promise(async function(resolve, reject) {
  console.log('running geek1');
  await sleep(2000);
  resolve('timeout geek1');
});

const p3 = new Promise(async function(resolve, reject) {
  console.log('running geek3');
  await sleep(2000);
  resolve('geek3');
});
const p4 = function() {
  return new Promise(function(resolve, reject) {
    console.log('running geek4');
    resolve('geek4');
  });
}
const p5 = new Promise(function(resolve, reject) {
  console.log('running geek5');
  resolve('geek5');
});

const runAllPromises = promises => Promise.all(promises.map(promise => typeof promise === 'function' ? promise() : promise));

runAllPromises([p1, p3, p4, p5]).then(console.log);

© www.soinside.com 2019 - 2024. All rights reserved.