I'm very confused with async/await. I've been reading answers on s/o but I do not understand if async/await actually does what I want.
I'm trying to return the result of an async call in a synchronous way and maybe that's why I fail, maybe it's not made for that? I do not want to return a callback (or promise) but the result of one.
Here's what I've been trying to do
let namespace = {};
namespace.Test = class{
constructor(){
}
async get(){
let response = await this._load();
let data = await response;
console.log(data); //Logs my data but return Promise instead
return data;
}
_load(){
let promise = new Promise((resolve, reject) => {
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(function(response){
resolve(response.json());
}).catch(error => reject(error));
});
return promise;
}
}
//The goal is to figure out if I can have that
let myTest = new namespace.Test();
//Here I want data NOT a promise
let res = myTest.get();
console.log(res); //logs a Promise but I want what has been resolved instead
I thought resolving the promise inside _load and then using await inside get would do that?
return. There’s no going back, sorry. :) - Ry-this._load()point toload()in the constructor which is empty or before constructor as private? - zer00neget async retrieve() {...not 100% about theasyncpart - zer00ne