Yelp Business Details

node v8.11.1
version: 5.0.0
endpointsharetweet
const axios = require('axios'); // promised based requests - like fetch() const { forEach } = require('p-iteration'); const apitoken = 'POhW9bxB9a0wTN9ysAXj__TpOFvuyw4FEhfykgL9hfqmfnrIOiR-1eyCqiwAUq_IY5QbGKkWtodHMBYvFbCcfZDbMNw81-hdC_t0-MUdfvJq6xgz1Jqu6ESOWbDUWnYx' function getCoffee() { return new Promise(resolve => { setTimeout(() => resolve('☕'), 2000); // it takes 2 seconds to make coffee }); } function getBus(tokenStr) { const AuthStr = 'Bearer '.concat(tokenStr); return axios.get('https://api.yelp.com/v3/businesses/search?term=coffee&latitude=32.786882&longitude=-117.399972', { headers: { Authorization: AuthStr } }) } async function go(token) { try { const coffee = await getCoffee(); console.log(coffee); // ☕ const bus = await getBus(token); console.log(bus.data); let deets = []; await asyncForEach(bus.data.businesses, async (bus) => { // await console.log(bus); const AuthStr = 'Bearer '.concat(token); const response = await axios.get(`https://api.yelp.com/v3/businesses/${bus.id}`, { headers: { Authorization: AuthStr } }); // console.log(response); deets.push(response); }); console.log(deets); } catch (e) { console.error(e); // 💩 } }
async function asyncForEach(array, callback) { for (let index = 0; index < array.length; index++) { await callback(array[index], index, array) } }
go(apitoken);
Loading…

no comments

    sign in to comment