Promiseは両目がぼんやりしていますから、両目が輝いています.(3)-Promiseのいくつかの重要な問題(一)
3707 ワード
どうやってPromiseの状態を変えますか?
https://github.com/hnt815/promise
promise ?
promise , pending,revolved( fulfilled),rejected ,
resolve(value) , pending resolved
reject(reason) Error , pending rejected
** **: promise , ,
const p = new Promise((resolve, reject) => {
// resolve(1) // promise resolved
// reject(2) // promise rejected
// throw new Error(' ') // , promise rejected , reason error
throw 3 // , promse rejected , reason 3
})
p.then(
value => {
console.log('value1', value)
},
reason => {
console.log('reason1', reason)
}
)
p.then(
value => {
console.log('value2', value)
},
reason => {
console.log('reason2', reason)
}
)
p.then(
value => {
console.log('value3', value)
},
reason => {
console.log('reason3', reason)
}
)
プロミセの状態を変えて、コールバック関数を指定します.誰が先ですか? promise ?
1. , ,
2. ?
1). executor resolve()/reject()
2). then()
3. ?
1). , , ,
2). , , ,
// : ,
new Promise((resolve, reject) => {
setTimeout(() => {
//1
resolve(1) // ( ),
}, 1000);
}).then(// ,
value => {
console.log('value', value)
},
reason => {
console.log('reason', reason)
}
)
// ,
new Promise((resolve, reject) => {
// ( )
resolve(1)
}).then(
// ,
value => {
console.log('value2', value)
},
reason => {
console.log('reason2', reason)
}
)
console.log('-------')
// ,
const p = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(1)
}, 1000);
})
setTimeout(() => {
p.then(
value => {
console.log('value3', value)
},
reason => {
console.log('reason3', reason)
}
)
}, 2000);
promise.then()に戻る新たなプロモーションの結果の状態はどうなりますか?promise.then() promise ?
1. : then()
2. :
1). , promise rejected, reason
2). promise , promise resolved, value
3). promise, promise promise
new Promise((resolve, reject) => {
resolve(1)
// reject(1)
}).then(
value => {
console.log('onResolved1()', value)
// return 2
// return Promise.resolve(3)
// return Promise.reject(4)
// throw 5
},
reason => {
console.log('onRejected1()', reason)
// return 2
// return Promise.resolve(3)
// return Promise.reject(4)
throw 5
}
).then(
value => {
console.log('onResolved2()', value)
},
reason => {
console.log('onRejected2()', reason)
}
)
, :
onResolved1() 1
onResolved2() undefined
: resolve(1) , promise resolved, .then onResolved , 1.
console.log() undefined,undefined Error Promise , .then() onResolved , undefined
コードは同期してgithubに更新されました.https://github.com/hnt815/promise