Promise对象用于表示一个异步操作的最终状态(完成或失败),以及其返回的状态。
通过then来绑定成功、失败的回调函数。
promise例子:
promise1里面的是setTimeout即300毫秒之后再执行resolve('foo')
var promise1 = new Promise(function (resolve, reject) {
setTimeout(function () {
resolve('foo');
}, 300)
});
promise1.then(function (value) {
console.log(value);
});
console.log(promise1);
答案:
promise1
foo
async用来声明一个异步的function,await用于等待一个异步任务执行完成的结果,await是一个操作符,即await后面是一个表达式。
async返回的是一个promise对象。
async function testAsync(){
return 'Hello async';
}
const data = testAsync();
console.log(data);
data.then(function(value){
console.log(value);
})
答案:
Promise{<fullfilled>:'Hello async'}
Hello async
Promise是异步操作,即在没有await的情况下执行async函数,它会立即执行,不会阻塞。
async没有返回值时,它会返回Promise{<fullfilled>:undefined},promise.then里面的value是undefined
async function testAsync(){
setTimeout(function(){
console.log(1)
},1000)
}
const data = testAsync();
console.log(data);
答案:
Promise{<fullfilled>:undefined}
1
await关键字会暂停当前async函数的执行
await操作符后面的表达式不是一个Promise,则返回本身。
所以结果是先打印Promise,再打印Hello await
await后面的表达式不是Promise的情况,是一个常量。
async function testAsync(){
const data = await 'Hello await';
return data;
}
testAsync().then(function(value){
console.log(value)
})
答案:
Hello await
await关键字会暂停当前async函数的执行
await操作符后面的表达式是一个Promise,则返回Promise的resolve的值。
所以结果是先打印Promise,再打印Hello await
await后面的表达式是一个Promise的情况。
function say() {
return new Promise(function (resolve, reject) {
setTimeout(function () {
let age = 26;
resolve(`Hello, age is ${age}`);
}, 1000);
})
}
async function demo() {
const v = await say();
console.log(v);
}
console.log(demo());
答案:
Promise
Hello,age is 26
async function async1() {
console.log('async1 start')
await async2()
console.log('async1 end')//添加到微任务
}
async function async2() {
console.log('async2')
}
console.log('script start')
setTimeout(function () {
console.log('setTimeout')//添加到宏任务中
}, 0)
async1();
new Promise(function (resolve) {
console.log('promise1')
resolve();
}).then(function () {
console.log('promise2')//添加到微任务
})
console.log('script end')
答案:
script start
async1 start
async2
promise1
script end
async1 end
promise2
setTimeout
async function async1() {
console.log('async1 start')
await async2()
console.log('async1 end')//添加到微任务
}
function async2() { // 去掉了 async 关键字 不确定
console.log('async2');
}
console.log('script start')
setTimeout(function () {
console.log('setTimeout')
}, 0)
async1();
new Promise(function (resolve) {
console.log('promise1')
resolve();
}).then(function () {
console.log('promise2')//添加到微任务
})
console.log('script end');
答案
script start
async1 start
async2
promise1
script end
async1 end
promise2
setTimeout
async function async1() {
console.log('async1 start')
await async2()
setTimeout(function () {
console.log('setTimeout1')//添加到第三个宏任务
}, 0)
}
async function async2() {
setTimeout(function () {
console.log('setTimeout2')//添加到第二个宏任务
}, 0)
}
console.log('script start')
setTimeout(function () {
console.log('setTimeout3')//添加到第一个宏任务
}, 0)
async1()
new Promise(function (r, j) {
console.log('Promise1')
r()
}).then(function () {
console.log('Promise2')//添加到微任务
})
console.log('script end')
答案:
script start
async1 start
Promise1
script end
Promise2
setTimeout3
setTimeout2
setTimeout1
async function t() {
var a = new Promise(
function (resolve, reject) {
console.log(1);
resolve(9);
console.log(2);
})
var b = new Promise(
function (resolve, reject) {
console.log(3);
resolve(8);
console.log(4);
})
console.log(5);
b = await b;
a = await a;
console.log(b);
console.log(a);
console.log(6);
}
t();
console.log(7);
答案:
1
2
3
4
5
7
8
9
6
延时函数
const setDelay = (millisecond) => {
return new Promise((resolve, reject) => {
if (typeof millisecond != 'number') reject(new Error('参数必须是number类型'));
setTimeout(() => {
resolve(`我延迟了${millisecond}毫秒后输出的`)//添加到第一个宏任务 添加到第三个宏任务
}, millisecond)
})
}
const setDelaySecond = (seconds) => {
return new Promise((resolve, reject) => {
if (typeof seconds != 'number' || seconds > 10) reject(new Error('参数必须是number类型,并且小于等于10'));
setTimeout(() => {
resolve(`我延迟了${seconds}秒后输出的,注意单位是秒`)//添加到第二个宏任务
}, seconds * 1000)
})
}
(async () => {
const result = await setDelay(1000);
console.log(result);
console.log(await setDelaySecond(2));
console.log(await setDelay(1000));
console.log('完成了');
})()
答案:
我延迟了1000毫秒后输出的
我延迟了2秒后输出的,注意单位是秒
我延迟了1000毫秒后输出的
完成了