0

estamos depurando una promesa pero tenemos problemas para poder usar el resultado Nuestra funcion es:

async function getFetch(urlForFetch) {
  var response = await fetch(`http://localhost:8081/_api/` + urlForFetch , {  
    accept: 'application/json;odata=verbose',
  })
  var bodyString = await response.text();
  console.log(typeof bodyString); //String
  return bodyString;
}

La misma logra traer el valor esperado en la variable bodyString . A la hora de utilizar la Promesa hacemos :

// spFetch es un objeto de tipo Promise
const spFetch = getFetch("/web/title");

El objeto spFetch es ahora una promesa y en consola se muestra asi:

introducir la descripción de la imagen aquí

o

 Promise {2hm2a20lrsa: 0, _state: undefined, _result: undefined, _subscribers: Array(0)}
 2hm2a20lrsa: 0_result: "<?xml version="1.0" encoding="utf-8"?><d:Title xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">Project Web App</d:Title>"_state: 1_subscribers: []lastIndex: (...)lastItem: (...)length: 0__proto__: Array(0)concat: ƒ concat()constructor: ƒ Array()copyWithin: ƒ copyWithin()entries: ƒ entries()every: ƒ every()fill: ƒ fill()filter: ƒ filter()find: ƒ find()findIndex: ƒ findIndex()flat: ƒ flat()flatMap: ƒ flatMap()flatten: ƒ flatten()forEach: ƒ forEach()includes: ƒ includes()indexOf: ƒ indexOf()join: ƒ join()keys: ƒ keys()lastIndex: (...)lastIndexOf: ƒ lastIndexOf()lastItem: (...)length: 0map: ƒ map()pop: ƒ pop()push: ƒ push()reduce: ƒ reduce()reduceRight: ƒ reduceRight()reverse: ƒ reverse()shift: ƒ shift()slice: ƒ slice()some: ƒ some()sort: ƒ sort()splice: ƒ splice()toLocaleString: ƒ toLocaleString()toString: ƒ toString()unshift: ƒ unshift()values: ƒ values()Symbol(Symbol.iterator): ƒ values()Symbol(Symbol.unscopables): {copyWithin: true, entries: true, fill: true, find: true, findIndex: true, …}get lastIndex: ƒ lastIndex()get lastItem: ƒ lastItem()set lastItem: ƒ lastItem(value)__proto__: Object__proto__: Object

Como ven en consola veo el resulado XML que me interesa acceder pero no he podido hacer uso de èl aun. Sugerencias?

1 respuesta 1

0

Intenta usar un await antes de llamar a tu función asíncrona para recuperar el resultado de tu promesa, así:

const spFetch = await getFetch("/web/title");
2
  • TKS @angy-qr [0] ERROR in ./src/taskpane/components/SpRest.js [0] Module parse failed: Cannot use keyword 'await' outside an async function [0] File was processed with these loaders: [0] * ./node_modules/react-hot-loader/webpack.js [0] * ./node_modules/babel-loader/lib/index.js [0] You may need an additional loader to handle the result of these loaders. [0] | } [0] | [0] > var spFetch = await getFetch("/web/title"); [0] @ ./src/taskpane/components/App.js [0] @ ./src/taskpane/index.js [0] @ multi react-hot-loader/patch ./src/taskpane/index.js [0] i 「wdm」: Failed to compile. el 19 nov. 2020 a las 23:56
  • tienes que agregar el 'async' a la función, donde tienes la linea de código, para que te acepte el await. Así como tu función 'getFetch' que tiene el async adelante 😃
    – Angy QR
    el 20 nov. 2020 a las 1:32

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.