resolve() 和 then() 比异步的东西先来
resolve() and then() comming first than async stuff
我读了一篇关于 promise
的指南,上面写着:
const promise = new Promise((resolve, reject) => {
// Do some async stuff
// If async opp successful
resolve();
// If async opp fails
reject();
});
所以,我正在尝试使用它,但没有成功:
let async = new Promise((resolve, reject)=>{
this.getPerson(page).subscribe( data => {
console.log("First")
});
resolve();
})
async.then((result) =>{
console.log("Last")
});
在控制台,我先是 "Last",有人给我提示吗?
您的 resolve();
应该在 this.getPerson(page).subscribe( data => {
之内
赞:
let async = new Promise((resolve, reject)=>{
this.getPerson(page).subscribe( data => {
console.log("First")
resolve(); // called asap the result gets from api and above line executed
});
// resolve(); will be called before the above async function get's its response
});
async.then((result) =>{
console.log("Last")
});
Note : Don't use variable name such async
, as it already reserved
keyword.
您正在查询完成之前解决承诺。检查下面的代码以获取正确的解析位置
let async = new Promise((resolve, reject)=>{
this.getPerson(page).subscribe( data => {
console.log("First");
resolve(); // this is the expected place
});
});
async.then((result) =>{
console.log("Last")
});
而不是使用 reserve async
关键字,在整个
let async = new Promise((resolve, reject)=>{
this.getPerson(page).subscribe( data => {
console.log("First")
});
resolve();
})
async.then((result) =>{
console.log("Last")
});
您可以将代码重写为:
const promise = new Promise((resolve, reject) => {
// Do some async stuff
// If async opp successful
setTimeout(function(){
resolve(console.log("First")
)});
});
promise.then((result) =>{
console.log("Last")
});
这应该可以解决您的问题。
我读了一篇关于 promise
的指南,上面写着:
const promise = new Promise((resolve, reject) => {
// Do some async stuff
// If async opp successful
resolve();
// If async opp fails
reject();
});
所以,我正在尝试使用它,但没有成功:
let async = new Promise((resolve, reject)=>{
this.getPerson(page).subscribe( data => {
console.log("First")
});
resolve();
})
async.then((result) =>{
console.log("Last")
});
在控制台,我先是 "Last",有人给我提示吗?
您的 resolve();
应该在 this.getPerson(page).subscribe( data => {
赞:
let async = new Promise((resolve, reject)=>{
this.getPerson(page).subscribe( data => {
console.log("First")
resolve(); // called asap the result gets from api and above line executed
});
// resolve(); will be called before the above async function get's its response
});
async.then((result) =>{
console.log("Last")
});
Note : Don't use variable name such
async
, as it already reserved keyword.
您正在查询完成之前解决承诺。检查下面的代码以获取正确的解析位置
let async = new Promise((resolve, reject)=>{
this.getPerson(page).subscribe( data => {
console.log("First");
resolve(); // this is the expected place
});
});
async.then((result) =>{
console.log("Last")
});
而不是使用 reserve async
关键字,在整个
let async = new Promise((resolve, reject)=>{
this.getPerson(page).subscribe( data => {
console.log("First")
});
resolve();
})
async.then((result) =>{
console.log("Last")
});
您可以将代码重写为:
const promise = new Promise((resolve, reject) => {
// Do some async stuff
// If async opp successful
setTimeout(function(){
resolve(console.log("First")
)});
});
promise.then((result) =>{
console.log("Last")
});
这应该可以解决您的问题。