untitled notebook

node v4.9.1
version: master
endpointsharetweet
'use strict' const request = require('request') try { search('javascript') .then( data => console.log('and the data', data), error => console.error('uh - oh', error) ); console.log('the query isn\'t done yet!'); } catch(err) { console.error(err); } finally { console.log('all done, or is it?') } function search(query) { return searchByString(query).then( data => { console.log('query complete') //console.log(JSON.stringify(data, null, 2)) return data }).catch( err => { console.log('ERROR') console.log(err) throw new Error(err.message) }) } function searchByString(query) { return new Promise( (resolve, reject) => { const url = `https://www.googleapis.com/books/v1/volumes?maxResults=40&fields=items(id,volumeInfo(title))&q=${query}` request.get(url, (err, res, body) => { if (err) { reject(Error('failed to make API call')) } const data = JSON.parse(body) resolve(data) }) }) }
Loading…

no comments

    sign in to comment