最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Returning angular promise from a function that resolves the promise very quickly - Stack Overflow

matteradmin6PV0评论

I am writing an asynchronous javascript function that will be called by consumers to get certain data. Following is the simple implementation that I wrote initially (error handing and other stuff removed for clarity).

function getData(callback){
    if (data is available as a JavaScript object){
        callback(data);
    }else{
        getAsyncData(function(data){
             //some transformations on data
             callback(data); 
        });
    }
}

What is important to note is that getData can return data quickly if data is already available as a JavaScript object.

I want to replace this implementation with the one that returns a promise object to the caller. This fiddle shows sample implementation - /

The question - Since getData can return quickly, can there be a possiblity where getData is resolving the promise even before caller has established handler chain using then method? Just to simulate this, in the fiddle if i call then method inside setTimeout function (with zero delay), callback doesn't get called. If i call the then method outside of the setTimeout function, callback gets called. I am not sure if this is even a valid concern or valid usecase. I am quite new to angularjs development and would appreciate your views :)

I am writing an asynchronous javascript function that will be called by consumers to get certain data. Following is the simple implementation that I wrote initially (error handing and other stuff removed for clarity).

function getData(callback){
    if (data is available as a JavaScript object){
        callback(data);
    }else{
        getAsyncData(function(data){
             //some transformations on data
             callback(data); 
        });
    }
}

What is important to note is that getData can return data quickly if data is already available as a JavaScript object.

I want to replace this implementation with the one that returns a promise object to the caller. This fiddle shows sample implementation - http://fiddle.jshell/ZjUg3/44/

The question - Since getData can return quickly, can there be a possiblity where getData is resolving the promise even before caller has established handler chain using then method? Just to simulate this, in the fiddle if i call then method inside setTimeout function (with zero delay), callback doesn't get called. If i call the then method outside of the setTimeout function, callback gets called. I am not sure if this is even a valid concern or valid usecase. I am quite new to angularjs development and would appreciate your views :)

Share Improve this question edited Jul 25, 2013 at 8:17 Vishwanath 6,0046 gold badges40 silver badges57 bronze badges asked Jul 25, 2013 at 7:59 Chintan SChintan S 981 silver badge8 bronze badges 2
  • just curious why you want to switch from callback to promise? – Davin Tryon Commented Jul 25, 2013 at 8:06
  • No critical reasons, except the added convenience for callers and an opportunity to use a different async programming style :) – Chintan S Commented Jul 25, 2013 at 8:58
Add a ment  | 

2 Answers 2

Reset to default 5

If you want getData() to return a $q promise instead of using a callback, I'd do the following refactor using $q.when() and usual $q.resolve():

function getData()
{
    if (data is available as a JavaScript object) {
        return $q.when(data); // resolves immediately
    } else {
        var q = $q.defer();
        getAsyncData(function(data){
             //some transformations on data
             q.resolve(data); 
        });
        return q.promise;
    }
}

No, a significant and important part of being a promise is that it doesn't matter when you attach the handler. Even if you create a promise now and resolve it immediately, then keep your puter running for the next 50 years, then attach a handler it will still fire.

All of this does assume that there isn't a bug/corner case in angularjs's promise implementation. If it doesn't work, it's a bug though.

If you ever need to know anything about how promises work, you can always refer to the Promises/A+ spec which angular adheers to. As a spec, it's one of the simplest and easiest to understand that I've e across (although I should mention that I've been involved in the spec for quite a while now).

Post a comment

comment list (0)

  1. No comments so far