I was trying to use Promise to test different ways of using Promise then i encountered this problems:
<code>const helloWorld = new Promise((resolve,reject) => {
setTimeout(() => {
let temp = true;
if(temp){
resolve("hello world 1");
}else {
reject("error occured");
}
},3000)
});
const helloWord2 = new Promise((resolve,reject) => {
setTimeout(() => {
let temp = true;
if(temp){
resolve("hello world 2");
}else{
reject("error occured");
}
},2000)
});
helloWorld.then((value) => {
console.log(value);
helloWord2.then((value) => {
console.log(value);
})
});
</code>
<code>const helloWorld = new Promise((resolve,reject) => {
setTimeout(() => {
let temp = true;
if(temp){
resolve("hello world 1");
}else {
reject("error occured");
}
},3000)
});
const helloWord2 = new Promise((resolve,reject) => {
setTimeout(() => {
let temp = true;
if(temp){
resolve("hello world 2");
}else{
reject("error occured");
}
},2000)
});
helloWorld.then((value) => {
console.log(value);
helloWord2.then((value) => {
console.log(value);
})
});
</code>
const helloWorld = new Promise((resolve,reject) => {
setTimeout(() => {
let temp = true;
if(temp){
resolve("hello world 1");
}else {
reject("error occured");
}
},3000)
});
const helloWord2 = new Promise((resolve,reject) => {
setTimeout(() => {
let temp = true;
if(temp){
resolve("hello world 2");
}else{
reject("error occured");
}
},2000)
});
helloWorld.then((value) => {
console.log(value);
helloWord2.then((value) => {
console.log(value);
})
});
actually, i thought that the result will wait for 3000ms then print “hello world 1” and then continuously wait for 2000ms to print “hello world 2” , but after print “hello world 1”, “hello world 2” immediately printed out.
can any experts explain for me why this happened? (tks)
New contributor
blablabla is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.