0

I'm trying to make a function that returns the body of an api call using a promise. My code is

function checkApi(link) {
    var promise = new Promise(function(resolve, reject) {
        //query api
    });
    promise.then(function(value) {
        console.log(value); //want to return this, this would be the body
    }, function(reason) {
        console.log(reason); //this is an error code from the request
    });
}

var response = checkApi('http://google.com');
console.log(response);

Instead of doing console log, I want to return the body of google.com so that I can use it. This is just a scope problem, but I'm not sure how to fix it. Thanks,

kjsmita6
  • 458
  • 1
  • 6
  • 21
  • 1
    So `return` it + `return` a promise itself. And, no, you cannot "unwrap" a promise result: as soon as you started dealing with promises - the only way to capture its contents is to use `.then()` – zerkms Aug 09 '15 at 22:34

1 Answers1

3

You can return the promise and then when you call the checkApi you can attach another .then().

function checkApi(link) {
    var promise = new Promise(function(resolve, reject) {
        //query api
    });
    return promise.then(function(value) {
        console.log(value); //Here you can preprocess the value if you want,
                            //Otherwise just remove this .then() and just 
        return value;       //use a catch()
    }, function(reason) {
        console.log(reason); //this is an error code from the request
    });
}

//presuming this is not the global scope.
var self = this;
checkApi('http://google.com')
    .then(function (value){
         // Anything you want to do with this value in this scope you have
         // to do it from within this function.
         self.someFunction(value);
         console.log(value)
    });
micnil
  • 4,705
  • 2
  • 28
  • 39
  • `.then(function (value){ response = value});` --- this code makes no sense. – zerkms Aug 09 '15 at 22:41
  • @zerkms checkApi now returns a promise. The line you are qouting is then used on that returned promise and sets the response. Why would this not work? – micnil Aug 09 '15 at 22:59
  • Because you're reassigning a variable in an asynchronous code, so it will never have the desired value outside the anonymous function. – zerkms Aug 09 '15 at 23:01
  • @zerkms the `response` variable is declared outside that anonymous function (in this case the global scope i guess). so when i'm assigning it the `value` in the anonymous function it will get the desired value. – micnil Aug 09 '15 at 23:08
  • That's right, but it will be *too late* since the anonymous function is executed asynchronously in this case. So even though it's technically assigned - there is no code that can consume it. Just try to add some code that as you think will `console.log` it. – zerkms Aug 09 '15 at 23:09
  • Ah okey now i understand what you mean. Yes you would not be able to do a console log of the response outside that `then()` function, because it would run before response is set. Ill update answer – micnil Aug 09 '15 at 23:16