promise对象的使用
let state = 1;
function step1(resolve, reject) {
console.log('1.洗菜');
if(state == 1) {
resolve('洗菜完成');
} else {
reject('洗菜错误');
}
}
function step2(resolve, reject) {
console.log('2.吃饭');
if(state == 1) {
resolve('吃饭完成');
} else {
reject('吃饭错误');
}
}
function step3(resolve, reject) {
console.log('3.洗碗');
if(state == 1) {
resolve('洗碗完成');
} else {
reject('洗碗错误');
}
}
new Promise(step1).then(function(val) { //new promise(step1).then().then().then()
console.log(val);
return new Promise(step2);
}).then(function(val) {
console.log(val);
return new Promise(step3);
}).then(function(val) {
console.log(val);
})
const Koa = require('koa') const app = new Koa() app.use( async ( ctx ) => { ctx.body = 'hello koa2' }) app.listen(3000) console.log('[demo] start-quick is starting at port 3000')