-1

Ive been reading about promises for a while now.. but when it came down to the real thing, i figured I cant find the correct syntax. I need your help, I dont understand something basic, please help me find it.

Here is my broken code:

var Q = require('es6-promise').Promise;

function timePrinting(num) {
    return function promice(resolve, reject) {

         /*time consuming action*/

        return resolve("Complete printing " + num + " seconds");
    }
}

Q = timePrinting(15).then(console.log(data), null);
  • This is way too broad for a single question, are you asking about how to [chain promises](http://stackoverflow.com/questions/23768933/chaining-nested-promises-in-a-loop) or about how to [convert setTimeout to a promise](http://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises) ? – Benjamin Gruenbaum Oct 26 '14 at 14:18
  • im just asking about the promise's syntax, the rest was eddited out. thanks – Misha Shblin Oct 26 '14 at 14:43
  • If you're asking how to convert something to a promise - please see the question I linked to: http://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises it should explain how to convert a callback to a promise. If "time consuming action" already returns a promise you can just `return timeConsumingAction(...)` or `return timeConsumingAction(...).then(...` – Benjamin Gruenbaum Oct 26 '14 at 14:50
  • it should return return a promise that should be resolved in the cascading .then
    Problem is it dosent work... There is something wrong with the syntax of the call or the logic upon which I return the promise from the function I cant figure it out :-/
    – Misha Shblin Oct 26 '14 at 14:54
  • It would be awesome if you created an isolated fiddle on jsfiddle.net illustrating the issue (or in stack snippets right here). As it currently stands it's hard to understand what you're asking here imo. – Benjamin Gruenbaum Oct 26 '14 at 14:56
  • The problem is I get my promise object by require. I cant figure out how to return a promise from inside the function so I could resolve it in the next .then statement here is my full code: http://hastebin.com/oterituwej.coffee – Misha Shblin Oct 26 '14 at 15:07
  • Sorry, your code indicates you're still very new to promises. This question as it is is too broad. You should consider a promises tutorial like the one the answerer linked to. – Benjamin Gruenbaum Oct 26 '14 at 15:20
  • Even though I think I understand the concept of promises quite well Im having hard time translating the concept to syntax.. that is what I wanted help with in the first place... Thanks a lot for your time :) – Misha Shblin Oct 26 '14 at 15:23

1 Answers1

0

What you are doing is returning a new function that takes two parameters.

To return a promise

var Promise = require('es6-promise').Promise;

function timePrinting(num) {
    return new Promise(function (resolve, reject) {   
        resolve("Complete printing " + num + " seconds");  // write this line when the promise should be fulfilled 
    });
}

With this, you can do

timePrinting(100).then(function (result) {
    console.log(result);
});

To log the string you passed to resolve

Good article about ES6 promises: http://www.html5rocks.com/en/tutorials/es6/promises/

XrXr
  • 2,027
  • 1
  • 14
  • 20
  • That would still not work, if you're performing `time consuming action` which is async this will return before that function is done. If on the other hand you do not want to wait for it and you want to return a synchronous result - you should not return a promise to begin with. – Benjamin Gruenbaum Oct 26 '14 at 14:54
  • I get: return new Promise(function (resolve, reject) { ^ ReferenceError: Promise is not defined – Misha Shblin Oct 26 '14 at 14:59
  • @BenjaminGruenbaum, This will not return when it's async.. The constructed promise object will be pending until `resolve` or `reject` is called. If you don't believe me, you can run the following code in FF http://hastebin.com/zuyujanive.js – XrXr Oct 26 '14 at 15:01
  • @MishaShblin, you need the first line `var Promise = require('es6-promise').Promise;` – XrXr Oct 26 '14 at 15:02
  • @XrXrXr I'm well aware of how promises work, thanks. What I mean is that calling `resolve` on the last line regardless of any async action is redundant and silly. Either return a plain value if you're timing a sync action or put `resolve` in the callback of the async action if you're not. – Benjamin Gruenbaum Oct 26 '14 at 15:04
  • it is my firs line :-/ I dont understand what you mean – Misha Shblin Oct 26 '14 at 15:04
  • @BenjaminGruenbaum, ah I see, that is not what I mean either, I just copied OP's code. I edited my answer to indicate this – XrXr Oct 26 '14 at 15:10
  • @MishaShblin, that is strange, `Promise` should be defined after the first line. Could you post your code somewhere? – XrXr Oct 26 '14 at 15:11
  • @MishaShblin You are calling `Promise` `Q`, it should be `var Promise = require('es6-promise').Promise;` Also you should be using `console.log` inside a lambda like in my answer when using the promise. My original code, `.then(console.log);` might not work – XrXr Oct 26 '14 at 15:15
  • I renamed the variables (even though I doubt it's the problem...) But I want the promise to resolve only after the loop has ended, so why would I want to put it in the setTimeout()? – Misha Shblin Oct 26 '14 at 15:20
  • @MishaShblin It doesn't necessarily mean you resolve inside the `setTimeout`. You call `resolve` when whatever action the promise represents finishes. In your code, you should be calling `resolve` once all the setTimout has finished - if that is your intention. – XrXr Oct 26 '14 at 15:26
  • that is what I intended to do... problem is it dosent work... Here is my full code again: http://hastebin.com/iyamaqirom.coffee – Misha Shblin Oct 26 '14 at 15:29
  • The line you call `setTimout` does not block execution; it completes near instantly. This means that your promise will resolve near instantly if you call `resolve` after the loop completes. `setTimout` will call the function you passed to it once the the timeout completes. You will have to structure your code so that `resolve` is called at the desired time. (which is fairly difficult if you want to keep the loop) – XrXr Oct 26 '14 at 15:35
  • @MishaShblin It would be nice if you could accept my answer :) – XrXr Oct 26 '14 at 20:51