当我在对象字面量中使用 await 时程序如何运行?
How the program runs when I use await in object literal?
当我写一个returns一个对象的函数,但是这个对象的每个值都是通过解析promise构造的,最后我会得到什么?我的意思是,对象值的类型是什么?
async foo() {
return {
p1: await fetch('http://dummy.restapiexample.com/api/v1/employees') // FETCH REQUEST,
p2: await fetch('http://dummy.restapiexample.com/api/v1/employees') // FETCH REQUEST
}
}
而这样的流程,p2
会在p1
之后解决吗?此代码是否与以下示例一样工作:
async foo() {
const p1 = await fetch('http://dummy.restapiexample.com/api/v1/employees') // FETCH REQUEST
const p2 = await fetch('http://dummy.restapiexample.com/api/v1/employees') // FETCH REQUEST
return {
p1,
p2
}
}
谢谢!
正在查看https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of immediately returning the final value, the asynchronous method returns a promise to supply the value at some point in the future.
p1 和 p2 return 两个示例中的承诺。
谢谢你打电话给我,让我澄清你的问题(!)。我会尽力解释您实际要问的内容。我很想简单地删除这个答案。它可能有帮助也可能没有帮助!
如果你伪造“FETCH REQUEST”是什么,使用 async
方法,它在启动时简单地打印到控制台,就在它(异步)解析之前,你可以很清楚地看到它将在开始 p2
之前完成 p1
。 运行 它几次证实了这一点,因为对 p1
和 p2
的调用不会交错。
async function foo() {
return {
p1: await randomAsyncMethod("p1"),
p2: await randomAsyncMethod("p2")
}
}
async function randomAsyncMethod(which){
console.log("starting",which);
return new Promise( resolve =>
setTimeout( () => {
console.log("resolving",which);
resolve();
}, Math.random() * 1000)
);
}
foo();
现在将其更改为第二个示例,您可以看到行为基本相同。
async function foo() {
var p1 = await randomAsyncMethod("p1");
var p2 = await randomAsyncMethod("p2");
return {
p1,
p2
}
}
async function randomAsyncMethod(which){
console.log("starting",which);
return new Promise( resolve =>
setTimeout( () => {
console.log("resolving",which);
resolve();
}, Math.random() * 1000)
);
}
foo();
至于你的对象会包含什么fetch
方法的return值是:
A Promise that resolves to a Response object.
当我写一个returns一个对象的函数,但是这个对象的每个值都是通过解析promise构造的,最后我会得到什么?我的意思是,对象值的类型是什么?
async foo() {
return {
p1: await fetch('http://dummy.restapiexample.com/api/v1/employees') // FETCH REQUEST,
p2: await fetch('http://dummy.restapiexample.com/api/v1/employees') // FETCH REQUEST
}
}
而这样的流程,p2
会在p1
之后解决吗?此代码是否与以下示例一样工作:
async foo() {
const p1 = await fetch('http://dummy.restapiexample.com/api/v1/employees') // FETCH REQUEST
const p2 = await fetch('http://dummy.restapiexample.com/api/v1/employees') // FETCH REQUEST
return {
p1,
p2
}
}
谢谢!
正在查看https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of immediately returning the final value, the asynchronous method returns a promise to supply the value at some point in the future.
p1 和 p2 return 两个示例中的承诺。
谢谢你打电话给我,让我澄清你的问题(!)。我会尽力解释您实际要问的内容。我很想简单地删除这个答案。它可能有帮助也可能没有帮助!
如果你伪造“FETCH REQUEST”是什么,使用 async
方法,它在启动时简单地打印到控制台,就在它(异步)解析之前,你可以很清楚地看到它将在开始 p2
之前完成 p1
。 运行 它几次证实了这一点,因为对 p1
和 p2
的调用不会交错。
async function foo() {
return {
p1: await randomAsyncMethod("p1"),
p2: await randomAsyncMethod("p2")
}
}
async function randomAsyncMethod(which){
console.log("starting",which);
return new Promise( resolve =>
setTimeout( () => {
console.log("resolving",which);
resolve();
}, Math.random() * 1000)
);
}
foo();
现在将其更改为第二个示例,您可以看到行为基本相同。
async function foo() {
var p1 = await randomAsyncMethod("p1");
var p2 = await randomAsyncMethod("p2");
return {
p1,
p2
}
}
async function randomAsyncMethod(which){
console.log("starting",which);
return new Promise( resolve =>
setTimeout( () => {
console.log("resolving",which);
resolve();
}, Math.random() * 1000)
);
}
foo();
至于你的对象会包含什么fetch
方法的return值是:
A Promise that resolves to a Response object.