/    Sign up×
Community /Pin to ProfileBookmark

Have a few doubts about Promises

Hello… I am coming from other programming languages and attempting to learn JS. One thing is bugging me regarding async/promises that I do not understand.

“`
function t1() {
console.log(‘In T1’);
for (let i = 0; i < 10000; i++) {
console.log(i);
}
return Promise.resolve(‘Finished T1’);
}

function t2() {
console.log(‘In T2’);
for (let i = 10000; i < 20000; i++) {
console.log(i);
}
return Promise.resolve(‘Finished T2’);
}

function main() {
t1().then((msg) => {
console.log(msg);
});
t2().then((msg) => {
console.log(msg);
});
console.log(‘R U N N I N G’);
}

main();

“`

The above script produces an output that is like

“`
in T1
0
1
2
3



9997
9998
9999
in T2
10000
10001
10002



19997
19998
19999
R U N N I N G
Finished T1
Finished T2
“`

However, I was expecting it to look more like

“`
in T1
in T2
R U N N I N G
(scattered/scrambled numbers here… could be anything between 0 < i < 10000 or 10000 < i < 20000
or the final Finished T1/Finished T2/RUNNING statements)
“`

I am used to threading, and I was expecting the numbers to be out of order etc. since two tasks are running at the same time.

Also, I was expecting to see ‘In T1’ and ‘In T2’ and then ‘R U N N I N G’ to be the first lines printed to stdout.

But as the actual output shows, everything looks very synchronous and order is preserved. ‘R U N N I N G’ isn’t printed until both functions finish etc.

Then after rewriting and returning an array of the digits in each Promise… I started to get the results that I was expecting:

“`
function t1() {
let x = [];
console.log(‘In T1’);
for (let i = 0; i < 10000; i++) {
x.push(i);
}
return Promise.resolve(x);
}

function t2() {
let y = [];
console.log(‘In T2’);
for (let i = 10000; i < 20000; i++) {
y.push(i);
}
return Promise.resolve(y);
}

function main() {
// V1
t1().then((msg) => {
console.log(msg);
});
t2().then((msg) => {
console.log(msg);
});
console.log(‘R U N N I N G’);

// V2
// Promise.all([t1(), t2()]).then((msg) => {
// console.log(msg);
// });
}

main();

“`

The output for the above script has the ‘In T1’, ‘In T2’, and ‘R U N N I N G’ messages showing first (I think the async is working… this is similar to what I was expecting to see in the first test):

“`
In T1
In T2
R U N N I N G
[
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
96, 97, 98, 99,
… 9900 more items
]
[
10000, 10001, 10002, 10003, 10004, 10005, 10006, 10007,
10008, 10009, 10010, 10011, 10012, 10013, 10014, 10015,
10016, 10017, 10018, 10019, 10020, 10021, 10022, 10023,
10024, 10025, 10026, 10027, 10028, 10029, 10030, 10031,
10032, 10033, 10034, 10035, 10036, 10037, 10038, 10039,
10040, 10041, 10042, 10043, 10044, 10045, 10046, 10047,
10048, 10049, 10050, 10051, 10052, 10053, 10054, 10055,
10056, 10057, 10058, 10059, 10060, 10061, 10062, 10063,
10064, 10065, 10066, 10067, 10068, 10069, 10070, 10071,
10072, 10073, 10074, 10075, 10076, 10077, 10078, 10079,
10080, 10081, 10082, 10083, 10084, 10085, 10086, 10087,
10088, 10089, 10090, 10091, 10092, 10093, 10094, 10095,
10096, 10097, 10098, 10099,
… 9900 more items
]
“`

So why does the first example appear to be synchronous… ie. happening in order?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@boohooOct 10.2021 — It will help you to understand all of it if you read about the Event Loop in JavaScript. The order of tasks is perfectly predictable, but may not be super intuitive at first.
Copy linkTweet thisAlerts:
@daveyerwinOct 11.2021 — > @TheHost#1638051 I am used to threading, and I was expecting the numbers to be out of order etc. since two tasks are running at the same time.

in the code you presented

you have not created any new threads
Copy linkTweet thisAlerts:
@js-guruOct 11.2021 — Very interesting.

Im JavaScript developer. I also think **async/await/promise** is very important in JavaScript. You are saying you are coming from other programming languages and attempting to learn JavaScript but you have already realized that **async/await/promise** is very important.

Your doubts come from you thinking that Promise is thread.

Promise is not thread and there is no thread in JavaScript.

So **t2** is executed after **t1** was executed. It means that **for loop**(10000~20000) in **t2** is executed after **for loop**(0-10000) in **t1** was executed.

``javascript<i>
</i>function t1() {
return new Promise((resolve, reject) =&gt; {
console.log('In T1');
setTimeout(() =&gt; {resolve('Finished T1')}, 5000);
});
}

function t2() {
return new Promise((resolve, reject) =&gt; {
console.log('In T2');
setTimeout(() =&gt; {resolve('Finished T2')}, 1000)
});
}

function main() {
t1().then((msg) =&gt; {
console.log(msg);
});
t2().then((msg) =&gt; {
console.log(msg);
});
console.log('R U N N I N G');
}

main();<i>
</i>
`</CODE>
The output for the above script is below.
<CODE lang="text">
`text<i>
</i>In T1
In T2
R U N N I N G
Finished T2
Finished T1<i>
</i>
``

In the above script, **setTimeout(() => {resolve('Finished T1')}, 5000)** in **t1** is executed first and then **setTimeout(() => {resolve('Finished T2')}, 1000)** in **t2** will be executed.

But the delay in **t1** is 5000ms. It is bigger than the delay(1000ms) in **t2**. So "**Finished T2**" is displayed before "**Finished T1**".
×

Success!

Help @TheHost spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...