0

I've been trying to wrap my head around asynchronous programming and the use of promises. To help understand them, I've written some trivial nested code, but have run into a snag.

Here's the code: http://pastebin.com/hBtk9vER Make sure you install when library (npm install)

var when = require('when');

function promise() {
  console.log("Promise");
    promiseRead().then(function(string) {
    console.log("Inner promise");
    console.log(string);
    });
}

function promiseRead() {
  console.log("PromiseRead");
  return baz().then(function() {
    console.log("Inner Read");
    var deferred = when.defer();
    setTimeout(function() {
      deferred.resolve("Hello World");
    }, 5000);
  });
}

function baz() {
  console.log("BAZ");
  return when(true); 
}

promise();

My problem is that console.log(string) is undefined, when I was expecting it to be "Hello World" after promiseRead() resolves. Interestingly, when I remove the timeout, it works as expected. Can anybody help explain this, why the promise function is executing it's code before promiseRead() has finished timeout.

Much appreciated

b1nd
  • 370
  • 6
  • 12
  • 1
    I do not have time for a full answer, but in your `Inner Read` you don't return anything (no promise) so your `var deferred = when.defer();` at this place is useless. – t.niese Jan 16 '14 at 12:11
  • To add to that, I know you're experimenting, but promises are not much help if you don't use them in a structured way. The point of promises (IMO) is to organize async code linearly. You could make this even more convoluted by adding rejections and passing deferreds around, but I hope you can see now why it's important to keep promise-based code clean. – acjay Jan 16 '14 at 14:10

2 Answers2

2

It looks like you need to return your deferred object in promiseRead()

Jozef Dransfield
  • 524
  • 4
  • 11
  • Awesome, thanks. I was under the impression that `deferred.resolve()` would return a deferred object, but than obviously didn't think about the asynchronous nature of the language. So in my example, was `promiseRead` returning null, before the deferred resolved and the function above it on the stack, had no way of knowing that a promise had to be waiting? – b1nd Jan 17 '14 at 00:28
1

some updates

var when = require('when');

function promise() {
  console.log("Promise");
    promiseRead().then(function(string) {
    console.log("Inner promise");
    console.log(string);
    });
}

function promiseRead() {
  console.log("PromiseRead");
  return baz().then(function() {
    console.log("Inner Read");
    var deferred = when.defer();
    setTimeout(function() {
      deferred.resolve("Hello World");
    }, 5000);
    return deferred.promise;
  });
}

function baz() {
  console.log("BAZ");
  return when(true); 
}

promise();
Vlad Nikitin
  • 1,929
  • 15
  • 17
  • Is this an answer to your question? Is there still a problem within it? If it is an answer to your question then take the time to explain how you solved it. – t.niese Jan 16 '14 at 19:22