Kong / unirest-nodejs

Unirest in Node.js: Simplified, lightweight HTTP client library.
http://unirest.io/nodejs
MIT License
948 stars 167 forks source link

in MeteorJS : How to wrap in RVSP and chain using async await in Javascript? #90

Open scheung38 opened 8 years ago

scheung38 commented 8 years ago

How to wrap in a Promise and chain the status code?

import unirest from 'unirest';
var RSVP = require('rsvp');

const callAsync = RSVP.denodeify(Meteor.call);

Meteor.method({
async myPutFunction() {

var promise = await new RSVP.Promise(function (resolve, reject) {
var unirest = require("unirest");

var req = unirest("PUT", "https://myURL");

req.headers({
   "cache-control": "no-cache",
  "content-type": "application/json",
  "authorization": "Basic c2VjMxc6sdsXXXXXXXXX"
});

  req.send("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n<user>\r\n    <description>Test Description</description>\r\n    <identityGroupName>All Groups:TEMP CHANGE GROUP</identityGroupName>\r\n    <name>user</name>\r\n</user>");

req.end(function (res) {
  if (res.error) reject(res.error);

  resolve(this.response);
});

return promise;

}
 });

async test() {

const res1 = await callAsync('someFunctionThatReturnsValue1');  // This is ok
const res2 = await callAsync('someFunctionThatReturnsValue2', res1);  //This is ok, value  #ok 
const res3 = await callAsync('myPutFunction, res2) // status code 204 is not returned?
  .then(txt => console.log('txt is :', txt)
  .catch(reason => console.log('reason is : ', reason));

return res3;

}

Meteor.start({
  callAsync('test');
});

But I don't get the status code returned? But promise codes?

I20160714-11:12:18.100(1)? txt is : { _45: 0, I20160714-11:12:18.100(1)? _81: 3, I20160714-11:12:18.100(1)? _65: { _45: 0, _81: 0, _65: null, _54: null }, I20160714-11:12:18.100(1)? _54: null }

nijikokun commented 8 years ago

resolve(this.response)

shouldn't it be

resolve(res)