10

I'm using the promis module to return my json data from the request module, but every time i run it, it gives me this.

Promise { _45: 0, _81: 0, _65: null, _54: null }

I can't get it to work, any one know the problem? here is my code:

function parse(){
return new Promise(function(json){
    request('https://bitskins.com/api/v1/get_account_balance/?api_key='+api+'&code='+code, function (error, response, body) {
        json(JSON.parse(body).data.available_balance);
    });
});
}

console.log(parse());
Sam H
  • 521
  • 3
  • 8
  • 19
  • Promises return a promise, a contract that it will resolve (.then) or reject (.catch) with some data at a later point. I suggest reading up on them. Your logging the returned promise. Your request hasn't returned at that point. – ste2425 Jan 01 '17 at 00:22

3 Answers3

38

A promise is an object that serves as a placeholder for a future value. Your parse() function returns that promise object. You get the future value in that promise by attaching a .then() handler to the promise like this:

function parse(){
    return new Promise(function(resolve, reject){
        request('https://bitskins.com/api/v1/get_account_balance/?api_key='+api+'&code='+code, function (error, response, body) {
            // in addition to parsing the value, deal with possible errors
            if (err) return reject(err);
            try {
                // JSON.parse() can throw an exception if not valid JSON
                resolve(JSON.parse(body).data.available_balance);
            } catch(e) {
                reject(e);
            }
        });
    });
}

parse().then(function(val) {
    console.log(val);
}).catch(function(err) {
    console.err(err);
});

This is asynchronous code so the ONLY way you get the value out of the promise is via the .then() handler.

List of modifications:

  1. Add .then() handler on returned promise object to get final result.
  2. Add .catch() handler on returned promise object to handle errors.
  3. Add error checking on err value in request() callback.
  4. Add try/catch around JSON.parse() since it can throw if invalid JSON
jfriend00
  • 683,504
  • 96
  • 985
  • 979
8

Use request-promise:

var rp = require('request-promise');

rp('http://www.google.com')
    .then(function (response) {
        // resolved
    })
    .catch(function (err) {
        // rejected
    });
Oded Breiner
  • 28,523
  • 10
  • 105
  • 71
0
// https://www.npmjs.com/package/request
const request = require('request')

/* --- */

/* 
    "Producing Code" (May take some time) 
 */
let myPromise = new Promise( function( resolve, reject ) {

    request( 'https://example.com', function ( error, response, body ) {

        if ( error ) return reject( error )
        
        try {

            resolve( body )
            
        } catch ( error ) {
            
            reject( error )
            
        }
        
    } )
    
} )

/* --- */

/* 
    "Consuming Code" (Must wait for a fulfilled Promise) 
 */
myPromise.then(
    
    function( success ) { 
        
        /* code if successful */ 
        console.log( success )
        
    },
    
    function( error ) { 
        
        /* code if some error */ 
        console.error( error )
        
    }
    
)
antelove
  • 3,216
  • 26
  • 20