javascript - Returning a value in function export using promises when second function is complete -


i'm trying return value after promise runs function once promises completed code below.

var promises = []; var userinfo = null; export function userrunner(userdata) {     userdata.foreach(function (obj) {         let x = fetch(apiurl1)             .then(function (data) {                 return data.json()             })         promises.push(x);          let y = fetch(apiurl2)             .then(function (data) {                 return data.json()             })         promises.push(y);     });      promise         .all(promises)         .then(function (results) {              return plotchart(results); //how return when it's done below?          }).catch(function (error) {             console.log(error);         });  }   function plotchart(obj){     //do work userinfo , return userinfo     return userinfo; } 

basically onces fetch complete , promise.all runs calls plotgraph function returns value. how return value when plotgraph complete?

im calling function js page using es6 this

import {     userrunner } './graph' 

any appreciated.

you can return promise chain component:

export function userrunner(userdata) {     return promise         .all(promises)         .then(function (results) {             return plotchart(results);         }) } 

and continue chain in importing file:

import { userrunner } './graph'  userrunner(userdata).then(function (chartedresults) {}) 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -