javaScript如何在第二个promise之前执行第一步
这似乎是一个愚蠢的问题,但我是这个话题的新手。在下面的脚本中,我有两个承诺。到目前为止,“第二宣传”首先执行,然后执行“ 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Quentin的答案是正确的:您传递给
新的Promise
的功能立即发生。但是:由于您已经在async
函数中拥有此功能,因此您可以等待
承诺。这会暂停async
函数,直到承诺解析为止,因此与您的功能不同,请使用Promise.all
等待您的显式new Promise> New Promise
并行,我的功能等待这些承诺串行。此外,如果您想要
新的Promise
您写的构造要等待在settimeout
中等待操作,则需要等待并调用resolve> resolve
settimeout
呼叫的回调,而不是在他们外面。Quentin's answer is correct: the function you pass to
new Promise
happens immediately. However: because you already have this in anasync
function, you canawait
Promises within it. This pauses theasync
function until the Promise resolves, so unlike your function withPromise.all
that waits for your explicitnew 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 insetTimeout
, you need to wait and call theresolve
method within the callback thatsetTimeout
calls, not outside them as you have it.你不能执行承诺。
您可以执行功能。
如果将函数传递给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 theresolve
function is called).