Skip to content

Instantly share code, notes, and snippets.

@qleguennec
Created August 16, 2017 14:29
Show Gist options
  • Save qleguennec/d117189c987db26252517d859a588b4c to your computer and use it in GitHub Desktop.
Save qleguennec/d117189c987db26252517d859a588b4c to your computer and use it in GitHub Desktop.
if (!_.has(this.cache, page)) {
const requestWithAttributes =
_.reduce(
_.toPairs(requestAttributes)
, (acc, [a, b]) => acc + a + "=" + b + "&"
, request(userInfo) + "?");
fetch(requestWithAttributes)
.then((resp) => resp.json())
.then((result) => this.cache[page] = result);
console.log(this.cache);
/* Object
* 0 : Array[30]*/
console.log(this.cache[page]);
/* undefined*/
}
@bpetetot
Copy link

le code est asynchrone dans ton fetch :

fetch(requestWithAttributes)
        .then((resp) => resp.json())
        .then((result) => {
            this.cache[page] = result
            console.log(this.cache);
            console.log(this.cache[page]);
        });

@bpetetot
Copy link

avec async / await :

const maFonction = async () => {
  const resp = await fetch('./data')
  const result = await resp.json()
  this.cache[page] = result
  console.log(this.cache);
  console.log(this.cache[page]);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment