javaScript如何在第二个promise之前执行第一步

发布于 2025-01-28 03:57:36 字数 1338 浏览 3 评论 0原文

这似乎是一个愚蠢的问题,但我是这个话题的新手。在下面的脚本中,我有两个承诺。到目前为止,“第二宣传”首先执行,然后执行“ firstpromise”。因为在“第二名”中,我设定了更少的时间。但是,如何首先执行“第一步”,完成此操作后,开始执行“第二赞美”? 如何重写下面的脚本

(async function()
{
//var final = new Array();
var final;
const firstPromise = new Promise(
function(resolve)
{
    let result = 2 + 2;
    resolve(result);
    setTimeout(() => console.log("please show me first"), 2000);
});
const secondPromise = new Promise(
function(resolve)
{
    let result2 = 0;
    resolve(result2 + 1);
    setTimeout(() => console.log("please show me second"), 1000);
});
var myP = Promise.all([firstPromise, secondPromise]).then((values) => {
    return values[0]+values[1];
  });
return myP;
})();

(async function()
{
//var final = new Array();
var final;
const firstPromise = new Promise(
function(resolve)
{
    let result = 2 + 2;
    resolve(result);
    setTimeout(() => console.log("please show me first"), 2000);
});

const secondPromise = new Promise(
function(resolve)
{
    let result2 = 0;
    resolve(result2 + 1);
    setTimeout(() => console.log("please show me second"), 1000);
});

var myP = Promise.all([firstPromise, secondPromise]).then((values) => {
    return values[0]+values[1];
  });
return myP;
})();

This might seem a silly question, but I am a newbie in this topic. In the script below i have two promises. By now "secondPromise" executing first, and then executing "firstPromise". Cuz in the "secondPromise" i set less time. But how to execute "firstPromise" first, after finished that, start executing the "secondPromise"?
How to rewrite the script below

(async function()
{
//var final = new Array();
var final;
const firstPromise = new Promise(
function(resolve)
{
    let result = 2 + 2;
    resolve(result);
    setTimeout(() => console.log("please show me first"), 2000);
});
const secondPromise = new Promise(
function(resolve)
{
    let result2 = 0;
    resolve(result2 + 1);
    setTimeout(() => console.log("please show me second"), 1000);
});
var myP = Promise.all([firstPromise, secondPromise]).then((values) => {
    return values[0]+values[1];
  });
return myP;
})();

(async function()
{
//var final = new Array();
var final;
const firstPromise = new Promise(
function(resolve)
{
    let result = 2 + 2;
    resolve(result);
    setTimeout(() => console.log("please show me first"), 2000);
});

const secondPromise = new Promise(
function(resolve)
{
    let result2 = 0;
    resolve(result2 + 1);
    setTimeout(() => console.log("please show me second"), 1000);
});

var myP = Promise.all([firstPromise, secondPromise]).then((values) => {
    return values[0]+values[1];
  });
return myP;
})();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

皇甫轩 2025-02-04 03:57:36

Quentin的答案是正确的:您传递给新的Promise的功能立即发生。但是:由于您已经在async函数中拥有此功能,因此您可以等待承诺。这会暂停async函数,直到承诺解析为止,因此与您的功能不同,请使用Promise.all等待您的显式new Promise> New Promise并行,我的功能等待这些承诺串行。

此外,如果您想要新的Promise您写的构造要等待在settimeout中等待操作,则需要等待并调用resolve> resolve settimeout呼叫的回调,而不是在他们外面。

console.log("start");
(async function () {
    const firstValue = await new Promise(
        //             ^^^^^
        function (resolve) {
            let result = 2 + 2;
            setTimeout(() => {
                console.log("please show me first");
                resolve(result);  // Promise resolves after 2000ms
            }, 2000);
        });

    const secondValue = await new Promise(
        //              ^^^^^
        function (resolve) {
            let result2 = 0;
            setTimeout(() => {
                console.log("please show me second");
                resolve(result2 + 1);
            }, 1000);
        });
    // These are values, not promises, because you await them.
    // However, the async function still returns a Promise, because
    // "async" covers the asynchronicity of the Promises you await.
    return firstValue + secondValue;
})().then(x => console.log(x));  // or .then(console.log)

Quentin's answer is correct: the function you pass to new Promise happens immediately. However: because you already have this in an async function, you can await Promises within it. This pauses the async function until the Promise resolves, so unlike your function with Promise.all that waits for your explicit new Promise Promises in parallel, my function waits for those Promises serially.

Furthermore, if you want the new Promise constructions you wrote to wait for the action in setTimeout, you need to wait and call the resolve method within the callback that setTimeout calls, not outside them as you have it.

console.log("start");
(async function () {
    const firstValue = await new Promise(
        //             ^^^^^
        function (resolve) {
            let result = 2 + 2;
            setTimeout(() => {
                console.log("please show me first");
                resolve(result);  // Promise resolves after 2000ms
            }, 2000);
        });

    const secondValue = await new Promise(
        //              ^^^^^
        function (resolve) {
            let result2 = 0;
            setTimeout(() => {
                console.log("please show me second");
                resolve(result2 + 1);
            }, 1000);
        });
    // These are values, not promises, because you await them.
    // However, the async function still returns a Promise, because
    // "async" covers the asynchronicity of the Promises you await.
    return firstValue + secondValue;
})().then(x => console.log(x));  // or .then(console.log)

南烟 2025-02-04 03:57:36

你不能执行承诺。

您可以执行功能。

如果将函数传递给Promise构造函数,则将立即(承诺构造函数)执行它。

没有办法延迟执行函数传递给承诺构造函数。

您可以将调用打到新的Promise中,然后仅在解决第一个承诺时才调用第二个功能。

(但是,请注意,在您的示例中, resolve函数在settimeout发生的调用中调用)。

You can't execute a promise.

You can execute a function.

If you pass a function to a Promise constructor, then it will be executed (by the Promise constructor) immediately.

There is no way to delay the execution of a function passed to a Promise constructor.

You could wrap your calls to new Promise in functions, and then only call the second function when the first promise is resolved.

(Note, however, that in your examples the calls to setTimeout happen after the resolve function is called).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文