一、直接返回Promise对象
function Example1() {
return new Promise((res, rej) => {
setTimeout(() => {
res(Math.random())
}, 1000);
})
}
二、 使用Promise.resolve返回固定对象
function Example2() {
return Promise.resolve({
number: Math.random()
})
}
三、使用async + await 返回Promise对象
function Example1() {
return new Promise((res, rej) => {
setTimeout(() => {
res(Math.random())
}, 1000);
})
}
async function Example2() {
let result = null;
await Example1().then(res => {
result = res;
})
return result
}
/**
* 需要注意
* await只能在async函数内使用
* await 命令后面是一个 Promise 对象,返回该对象的结果。如果不是 Promise 对* 象,就直接返回对应的值。
/

