2

I have this async function that I want to turn into a promise

    var myAsyncFunction = function(err, result) {
        if (err) 
            console.log("We got an error");


        console.log("Success");
    };

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

and I get TypeError: Cannot call method 'then' of undefined.

What is wrong with this code?

reza
  • 5,972
  • 15
  • 84
  • 126
  • I am using q for my promise package – reza Oct 21 '13 at 21:45
  • Your function doesn't return anything, what are you trying to do – Esailija Oct 22 '13 at 00:16
  • Your function does not look very asynchronous, but more like a callback for an async function. Which one are you calling? – Bergi Oct 22 '13 at 09:06
  • I am trying to understand/learn how to use promises to avoid callback hell. So in order to understand that concept, I wrote this example code. I thought this would be a simple example; a template for a callback... That is the motivation behind the example. Since I do no have a return value for myAsyncFunction, even if I change the code to function(err), I still get TypeError: Cannot call method 'then' of undefined – reza Oct 22 '13 at 15:52
  • If you just give us a template for a callback, we can only give you a template on what to do with a promise: `.then(console.log.bind(console, "Success"), console.log.bind(console, "We got an error"))`. But how to *use* that callback or promise method, you first need a function that is async. And here is where the basic differences between [callback style and promises](http://stackoverflow.com/q/22539815/1048572) are. – Bergi Jul 12 '14 at 21:19
  • duplicate of [How do I convert an existing callback API to promises?](http://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises) - at least based on the title of the question – Bergi Jul 12 '14 at 21:20

2 Answers2

4

There are various ways in Q:

Q.nfcall(myAsyncFunction, arg1, arg2);
Q.nfapply(myAsyncFunction, [arg1, arg2]);

// Work with rusable wrapper
var myAsyncPromiseFunction = Q.denodeify(myAsyncFunction);
myAsyncPromiseFunction(arg1, arg2);

in Deferred implementation:

var myAsyncPromiseFunction = deferred.promisify(myAsyncFunction);
myAsyncPromiseFunction(arg1, arg2);

One notable difference: Wrappers as generated by Deferred additionally auto-resolve promises passed as an arguments, so you can do:

var readFile = deferred.promisify(fs.readFile);
var writeFile = deferred.promisify(fs.writeFile);

// Copy file
writeFile('filename.copy.txt', readFile('filename.txt'));
Mariusz Nowak
  • 32,050
  • 5
  • 35
  • 37
-2

myAsyncFunction return nothing(undefined actually) in your code.

If you use whenjs, the normal way will be like this:

var myAsyncFunction = function() {

    var d = when.defer();

    //!!!do something to get the err and result

    if (err) 
       d.reject(err);
    else
       d.resolve.(result);

    //return a promise, so you can call .then
    return d.promise;
};

Now you can call:

myAsyncFunction().then(function(result(){}, function(err){});
Andrew
  • 5,290
  • 1
  • 19
  • 22
  • 2
    Your code looks like is just doing the [deferred anti-pattern](https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns) – Esailija Oct 22 '13 at 16:21