Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Attach request result to json response

So this is the code:

let pending_task_list = fetch("/tasks", {
    headers: {
        "Accept": "application/json"
    }
}).then(function(res) {
    return {succeeded: res.ok, pending_message: res.json()};
}).then(function(data){
    let message = data.pending_message;
    return {succeeded: data.succeeded, message: message};
});

let task_list = await pending_task_list;

Now, task_list.message is still a promise. How can resolve the promise given by res.json() in this case.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

edit your code like this

let pending_task_list = fetch("/tasks", {
    headers: {
        "Accept": "application/json"
    }
}).then(function(res) {
    return {succeeded: res.ok, pending_message: res.json()};
}).then(async function(data) {
    let message = await data.pending_message; // Await the promise resolution
    return {succeeded: data.succeeded, message: message};
});


let task_list = await pending_task_list;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading