node.js and promise

404 views
Skip to first unread message

Reza Razavipour

unread,
Oct 18, 2013, 6:11:47 PM10/18/13
to nod...@googlegroups.com
I am a fairly new to javaScript and node.js both.

I am trying to wrap my head around promises and I am having a hard time. I am looking for a very simple tutorial/video on this topic and having little success.

Is there a reference that can be recommended? A before and after code snippet can be a good starting position.

Reza


Gorgi Kosev

unread,
Oct 18, 2013, 6:34:15 PM10/18/13
to nod...@googlegroups.com

Try http://spion.github.io/promise-nuggets - its not complete yet but the main concepts are covered

--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Angel Java Lopez

unread,
Oct 18, 2013, 6:45:58 PM10/18/13
to nod...@googlegroups.com

Jason Crawford

unread,
Oct 19, 2013, 2:02:49 AM10/19/13
to nod...@googlegroups.com

Qihan Zhang

unread,
Oct 18, 2013, 6:22:50 PM10/18/13
to nod...@googlegroups.com
Hi Reza,

I was just trying to wrap my head around how promises work yesterday and I highly recommend this article:


I'm pretty new to node.js as well and found the explanation here very easy to understand.

Also, I would take a look at the documentation on https://github.com/kriskowal/q

Thanks,

Mariusz Nowak

unread,
Oct 19, 2013, 6:33:50 AM10/19/13
to nod...@googlegroups.com
Reza, check this before and after example: https://github.com/medikoo/deferred#plain-nodejs-callbacks-approach and once I tried explain promises to newcomers with following slides: http://www.medikoo.com/asynchronous-javascript/ it's nearly two years old, but still mostly up to date. Maybe that'll be helpful.

Gary Katsevman

unread,
Oct 19, 2013, 2:42:57 PM10/19/13
to nod...@googlegroups.com
There is also the Promises/A+ spec itself [http://promisesaplus.com/] which is fairly readable and can give more insight into promises.

Gary Katsevman
gkatsev.com


Reza Razavipour

unread,
Oct 21, 2013, 1:52:38 PM10/21/13
to nod...@googlegroups.com
we all of this I still can not figure out how to take an async function and change it from callback to a promise.

1. Do I have to declare a promise before I can call a 'then' on it? Every time I do not and call a then on an async function, I get a syntax error.



On Friday, October 18, 2013 3:11:47 PM UTC-7, Reza Razavipour wrote:

Mariusz Nowak

unread,
Oct 21, 2013, 1:58:34 PM10/21/13
to nod...@googlegroups.com
On 21 paź 2013, at 19:52, Reza Razavipour <reza.ra...@gmail.com> wrote:

we all of this I still can not figure out how to take an async function and change it from callback to a promise.



1. Do I have to declare a promise before I can call a 'then' on it? Every time I do not and call a then on an async function, I get a syntax error.



On Friday, October 18, 2013 3:11:47 PM UTC-7, Reza Razavipour wrote:
I am a fairly new to javaScript and node.js both.

I am trying to wrap my head around promises and I am having a hard time. I am looking for a very simple tutorial/video on this topic and having little success.

Is there a reference that can be recommended? A before and after code snippet can be a good starting position.

Reza



--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
 
---
You received this message because you are subscribed to a topic in the Google Groups "nodejs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nodejs/Fx_SjgkXvKU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nodejs+un...@googlegroups.com.

Reza Razavipour

unread,
Oct 21, 2013, 3:57:38 PM10/21/13
to nod...@googlegroups.com
Let us use an example:



var q = require('q');

    var myAsyncFunction = function(err, result) {

    if (err) 

    console.log("We got an error");

   

   

    console.log("Success");

    };

    myAsyncFunction().then(function () { console.log("promise is working"); });


myAsyncFunction().then(function () { console.log("it really worked"); });

                      ^

TypeError: Cannot call method 'then' of undefined


How do I turn myAsyncFunction into a promise?



Mariusz Nowak

unread,
Oct 21, 2013, 4:09:25 PM10/21/13
to nod...@googlegroups.com
var promisify = require('deferred').promisify;
var myAsyncFunctionPromiseVersion = promisify(myAsyncFunction);

myAsyncFunctionPromiseVersion().then(function () { console.log("it works!"); });

Reza Razavipour

unread,
Oct 21, 2013, 5:37:55 PM10/21/13
to nod...@googlegroups.com
wonderful... thank you.

and in q, what would that look like?

Regards,
Reza

Reza Razavipour

unread,
Oct 21, 2013, 5:50:46 PM10/21/13
to nod...@googlegroups.com
and what would that look like in q?
Is it Q.nfcall ?



On Friday, October 18, 2013 3:11:47 PM UTC-7, Reza Razavipour wrote:
Reply all
Reply to author
Forward
0 new messages