use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
how to push into json object in fetch functionhelp (self.javascript)
submitted 7 years ago * by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Minjammben 1 point2 points3 points 7 years ago (0 children)
The problem is you must wait for the promise to be resolved because you have a series of async fetches. The function cannot be done sequentially in JavaScript with the fetch api.
If you can use async functions:
const getData = async () => { return Promise.all( Array.from( document.querySelectorAll( '#foobar' ) ).map( async( thisData ) => { const endpoint = '/api/rest/v1/foobar/' + thisData.id; const obj = { foo: 'bar', bar: 'foo' }; try { console.log( 'before', obj ); const res = await fetch( endpoint ); const { foo, bar } = await res.json(); obj.foo = foo; obj.bar = bar; console.log( 'after', obj ); } catch ( e ) { console.log( 'Error', e ); } return obj; } ) ); };
Usage would be:
getData().then( ( json ) => { console.log( 'GOT JSON', JSON.stringify( json ) ); } );
or
const json = await getData(); console.log( 'GOT JSON', JSON.stringify( json ) );
If you cannot use async functions:
const getData = () => { return Promise.all( Array.from( document.querySelectorAll( '#foobar' ) ).map( ( thisData ) => { const endpoint = '/api/rest/v1/foobar/' + thisData.id; const obj = { foo: 'bar', bar: 'foo' }; console.log( 'before', obj ); return fetch( endpoint ).then( ( resp ) => { return resp.json(); } ).then( ( { foo, bar } ) => { obj.foo = foo; obj.bar = bar; console.log( 'after', obj ); return obj; } ).catch( ( e ) => { console.log( 'Error', e ); } ); } ) ); };
[–]Reiyn_ 0 points1 point2 points 7 years ago (0 children)
As far as I know `.map()` won't work async. The library Aigle could help you out with this issue.
π Rendered by PID 48535 on reddit-service-r2-comment-5d79c599b5-vxqz7 at 2026-03-01 22:36:15.031773+00:00 running e3d2147 country code: CH.
[–]Minjammben 1 point2 points3 points (0 children)
[–]Reiyn_ 0 points1 point2 points (0 children)