-2

I've an example from an online tutorial and I'm wondering how can I convert each then into async call?

Edit- Basically i want the promise to proceed with each call in an async fashion, or how it works out there. I'm new to promises, one promise mean single async call or a promise can have a several async calls and then callback at the end,

if i have following three function calls, how can I chain them to be async, right now they all fire immediately in a sequence?

Edit 2 - Improved my question based on learnings with Joel's support, I've a fiddle now http://jsfiddle.net/smartdev101/eLxxpjp3/

var q = require('q');
        var defer = q.defer();
        defer.promise
        .then(function(weapon){
            setTimeout(function(){console.log('wait')}, 2000);
            console.log("You can have my " + weapon);
            return "bow";
        })
        .then(function(weapon){
            console.log("and my " + weapon);
            return "axe";
        })
        .then(function(weapon){
            console.log("and my " + weapon);
        });

        defer.resolve("sword");
user2727195
  • 7,122
  • 17
  • 70
  • 118
  • 1
    You're going to have to be more specific about when you're trying to accomplish. Try using words to explain exactly when you want various things to run. I hope you realize that `setTimeout()` only schedules a callback for sometime in the future - it doesn't delay when the next line of code runs. – jfriend00 Oct 24 '14 at 15:14
  • In Javascript, you can't just convert a function call to async. A given operation is either synchronous or asynchronous. All your operations except the `setTimeout()` are synchronous. If you want to make a synchronous operation behave asynchronously, you can hide it behind a `setTimeout()`, but you'd have to add a setTimeout() for every operation. Is that really what you want to do? And, if so, how much of a delay do you want on each operation? – jfriend00 Oct 24 '14 at 15:21
  • Promises don't produce asynchronous code, they are used to handle existing asynchronous calls (such as IO). You must remember that javascript is single-threaded, so your code will be executed all in a go, even by using promises. If you want to introduce arbitrary delay between calls, you can call a setTimeout in each of your promise chaining. – Joel Oct 24 '14 at 15:23
  • above code may not be the right example and I'm new to promises, but I want some async procedures to be called asynchronously in a sequence, let's say calling a service (async), fetching data, parsing the results, and then inserting into database (again async) and then notifying with a final callback, can promises do that? if then how in the above case while feeding the result of one async call to the next – user2727195 Oct 24 '14 at 15:24
  • You need to do some more research then. Find a real example that uses actual asynchronous operations (such as the ones you mention) and uses promises to manage them and study it. When you have a specific question about how to do something beyond the example you find, then post a new question here. StackOverflow isn't really about teaching you a new topic (e.g. promises) from scratch. – jfriend00 Oct 24 '14 at 15:25
  • I couldn't find, been researching since yesterday, can you point to some examples with async procedures in sequence using promises – user2727195 Oct 24 '14 at 15:26
  • It takes seconds of Google searching to find many examples. Here's one to start with http://12devs.co.uk/articles/promises-an-alternative-way-to-approach-asynchronous-javascript/, but you should easily be able to find a dozen in minutes. – jfriend00 Oct 24 '14 at 15:28
  • thanks for the link, I'm working on the example – user2727195 Oct 24 '14 at 15:36
  • @jfriend00 please review this http://jsfiddle.net/smartdev101/eLxxpjp3/ – user2727195 Oct 24 '14 at 17:09
  • Review it for what? If you want to make a question out of that code, then please paste the code into a question and ask something very specific about that code. – jfriend00 Oct 24 '14 at 17:12
  • ok, here is it http://stackoverflow.com/questions/26553098/sequencing-async-operations-followed-by-onresult-call – user2727195 Oct 24 '14 at 17:24

1 Answers1

2

Maybe this should give you what you expect (although I'm not sure what you expect) :

var q = require('q');

function waitAndSpeak(words) {
    var defer = q.defer();
    setTimeout(function(){
        defer.resolve(words)
    }, 2000);
    return defer.promise;
}

waitAndSpeak("sword").then(function(weapon) {
    console.log("You can have my " + weapon);
    return waitAndSpeak("bow");
}).then(function(weapon) {
    console.log("and my " + weapon);
    return waitAndSpeak("axe");
}).then(function(weapon) {
    console.log("and my " + weapon);
}).done();
Joel
  • 2,374
  • 17
  • 26
  • hmmm close, Joel, this seems like the one, I'll basically put http service calls within each then, will return on how it looks like at the end – user2727195 Oct 24 '14 at 15:44
  • The key point here lies in method waitAndSpeak: the deferred is resolved only after the method returns. So that it emulates asynchronous call. – Joel Oct 24 '14 at 15:46
  • ok, and I can have something different for each subsequent thenables, for instance waitAndSpeak1, waitAndSpeak2, waitAndSpeak3, I'll post a fiddler shortly for your review and then accept your answer. – user2727195 Oct 24 '14 at 15:49
  • Thanks Joel for bearing with my example, sometimes being new to something makes it difficult to describe what you're looking for, along with some limitations on the way things work in my environment, please review this fiddle, I'll need more of your help, hopefully this would make it more clearer. http://jsfiddle.net/smartdev101/eLxxpjp3/ – user2727195 Oct 24 '14 at 16:51
  • can you please look at this now http://stackoverflow.com/questions/26553098/sequencing-async-operations-followed-by-onresult-call – user2727195 Oct 24 '14 at 17:53