API, SuperAgent and Promise - vijayetar/seattle-301d55 GitHub Wiki
SuperAgent
is light-weight progressive ajax API crafted for flexibility, readability, and a low learning curve after being frustrated with many of the existing request APIs. It also works with Node.js!
it is like a ajax
get this to understand the commands
const superagent = require('superagent'); app.get('/starwars',(require,response) => { let url = 'https://swapi.co/api/'; superagent.get(url) .then(results => { console.log ('1. I went to SWAPI'); }) console.log('2. I did not go'); console.log('3. I did not go either'); })
So instead of waiting for the results, .then is a promise that will come back to it, but the next two console.logs will be done first. And you can chain multiple .then statements or promises together.
Promise
- It has several advantages. It allows chaining. But it has pecularities. But you cannot switch resolve, reject.
let p = new Promise ((resolve,reject) => { let sum = 5+7; if (sum === 12) { resolve('Success'); } else { reject('Failure'); } }) console.log(p); p.then((message) => {console.log(
We are in the then, result: ${message})}) p.catch((message) => {console.log(We are in the catch, result: ${message})})
Promise.all
const promise1 = Promise.resolve(3); const promise2 = 42; const promise3 = new Promise(function(resolve, reject) { setTimeout(resolve, 100, 'foo'); });
Promise.all([promise1, promise2, promise3]).then(function(values) { console.log(values); }); // expected output: Array [3, 42, "foo"]