promise链式调用resolve的写法
有三种方式:
- 直接new一个新的Promise对象
- 通过Promise.resolve(data)的方式处理
- 直接返回数据处理,return data+操作
//1. 原始写法
let foo = new Promise((resolve) => {
setTimeout(() => {
resolve('aaa');
}, 1000)
});
foo.then(res => {
console.log(res);
return new Promise(resolve => {
resolve(res + 111);
})
}).then(res => {
console.log(res)
})
//2. 第二种写法
let foo = new Promise((resolve) => {
setTimeout(() => {
resolve('aaa');
}, 1000)
});
foo.then(res => {
console.log(res);
return Promise.resolve(res + 111)
}).then(res => {
console.log(res)
})
//3.第三种写法
let foo = new Promise((resolve) => {
setTimeout(() => {
resolve('aaa');
}, 1000)
});
foo.then(res => {
console.log(res);
return res + 111
}).then(res => {
console.log(res)
})
网友评论