arizafar
arizafar

Reputation: 3122

Promise waits to get resolve without return

My understanding of the async handling in nodejs/javascript is that if an async call is handled in a function it must return a promise or accept a callback for chaining as well as for waiting for the async call to get complete.

But I just find out it does not, as the following code works and wait for all the promises to get completed

function handlePromise() {
  Promise.resolve('Hello Async').then(data => {
    Promise.resolve('Hello Async 2').then(data => {
      return delay(3000).then(() => console.log(data));
    });
    return delay(2000).then(() => console.log(data));
  });

  Promise.resolve('hello').then(data => console.log(data))
};

function delay(time) {
  return new Promise(resolve => setTimeout(resolve, time))
}

function handlePromise2() {
  handlePromise()
};

handlePromise2();
It should work when I return the promises up to the end.

function handlePromise() {
	return Promise.resolve('Hello Async').then(data => {
		return Promise.resolve('Hello Async 2').then(data => {
			return delay(3000).then(() => console.log(data));
		});
	}).then(() => {
		return Promise.resolve('hello').then(data => console.log(data))
	}).then(() => {
		return Promise.resolve('hello').then(data => console.log(data))
	});

};

function delay(time) {
	return new Promise(resolve => setTimeout(resolve, time))
}

function handlePromise2() {
	return handlePromise()
};

handlePromise2();

Same works with the callbacks also

const fs = require('fs')
function handlePromise() {
    delay();
    console.log('sync')
};

function delay() {
    fs.writeFile('data.txt', 'Hello Async', () => console.log('done'));
}

handlePromise();

So what's I am missing out here?

If just calling a then with the promise resolves the promise then whats the point of async/await if I dont need the resolved value?

Upvotes: 0

Views: 936

Answers (1)

Naguib Ihab
Naguib Ihab

Reputation: 4496

If i understand your question right, you're saying that the first snippet should not work because it's not returning the promises and you're asking why is it working.

Short answer: It's not really working, handlePromise2() finished and returned without waiting on the promises to get resolved or rejected.

Long answer: It's like you going to the bakery and asking for a bread, but instead of waiting in line after asking for it you leave, the bread still gets baked but then it gets thrown away because the client (In our case that's handlePromise2) made the call and assumed that the work is finished, after all its scope was to just call that function.

Promises are used so that the client that's calling the function knows to expect something, so after you'd ask for the bread you'd wait for it to be finished, and that's called a promise it's not the actual value (aka bread) but a promise of a value. That's what you're doing in the second snippet.

And now poor handlePromise() doesn't know what to do with the food enter image description here

Upvotes: 3

Related Questions