0

So I have used .then to try and assign the value of promise to the object but I can't and now I tried a anonymous async function which isnt helping me.

import fetch from 'node-fetch'
import { setProps } from '../helpers/objectResponse.js';
if (!globalThis.fetch) {
    globalThis.fetch = fetch;
}

const wrapMe = (pokimon) => {
    let value;
    const getPokimon = async (pokimon) => {
    const data = await fetch(`https://pokeapi.co/api/v2/pokemon/${pokimon.toLowerCase()}`);
    const response = await data.json();
    return response
    }

    (async () => {
        const data = await getPokimon(pokimon);
        return value = data
    })
    return value

}```
  • @zero298 what I'm trying is to return the value as string or object I dont a callback function – haideralidev Sep 18 '21 at 22:06
  • "*assign the value of promise to the object*" - which object? And where are you calling `wrapMe`? No, you will never achieve a method that synchronously returns the data. All you can do is return a promise. Deal with that in the location where you call the function. – Bergi Sep 18 '21 at 23:14

2 Answers2

-1

the wrapMe me function need to be async

Amir Saadallah
  • 668
  • 1
  • 8
  • 19
-1

you must have to use callback or promise for ajax requests you cant simply call ajax and get its value for return

Return data.json() without using await so your promise will be returned to getPokimon

try doing this way

function getPokimon(pokimon) {
    return fetch(`https://pokeapi.co/api/v2/pokemon/${pokimon.toLowerCase()}`).then(r=>r.json())
}

getPokimon("ditto").then(r=>{
    console.log(r)
})
Amir Rahman
  • 1,076
  • 1
  • 6
  • 15