2016-01-30 1 views
2

Je travaille sur l'extraction de données à partir d'un serveur GraphQL et j'essaie d'implémenter les fonctions ES7 Async via babel. Je reçois actuellement undefined dans la console et je ne suis pas sûr de ce que je fais mal.Fonctions asynchrones renvoyant undefined

import fetch from 'isomorphic-fetch'; 
/** 
* [transport creates call to server with isomorphic-fetch] 
* @param {[String]} path  [url to hit with request] 
* @param {[Object]} query  [The GraphQL query/mutation] 
* @param {[Object]} queryParams = {} [Params to pass into query] 
* @return {[Promise]}   [Promise containing payload] 
*/ 
//function that returns a promise 
export function transport (path, query, queryParams = {}) { 
    return new Promise ((resolve, reject) => { 
     return fetch(path, { 
      method: 'POST', 
      headers: { 
       'Accept': 'application/json', 
       'content-type': 'application/json' 
      }, 
      body: JSON.stringify({ 
       query, 
       queryParams 
      }) 
     }) 
     .then(res => res.json()) 
     .then(response => { 
      if(response.errors) { 
      return error(response.errors); 
      } 
      return resolve(response.data); 
     }) 
     .catch(error); 
    }); 
} 
import { transport } from './utils/transport.js'; 

/** 
* [reachGraphQL Makes queres or mutations against GraphQL] 
* @param {[String]} path  [path to the GraphQL server] 
* @param {[Object]} query  [The query that GraphQL will use to fetch your data] 
* @param {[object]} queryParams = {} [Should contain object with different query params] 
* @return {[Object]}    [Data that was queried or mutated] 
*/ 
//Heres Where I'm awaiting a promise from the transport function 
export function reachGraphQL (path, query, queryParams = {}) { 
    async() => { 
    try{ 
     let response = await transport(path, query, queryParams); 
     return response; 
    } catch (error) { 
     console.log(error) 
    } 
    } 
} 
+0

Votre promesse implémente résoudre mais n'appelle pas rejeter sur erreur. Votre code pourrait-il avaler une exception? – orourkedd

+0

pas tout à fait sûr, je pense que c'est la manière dont j'ai utilisé async sur reachGraphQL() –

+0

Où est défini 'erreur'? – orourkedd

Répondre

1

Votre reachGraphQL définit simplement une fonction async de flèche, mais ne fait rien avec elle. Et il ne return rien. Plutôt, il devrait être async lui-même:

export async function reachGraphQL (path, query, queryParams = {}) { 
    try { 
     return await transport(path, query, queryParams); 
    } catch (error) { 
     console.log(error) 
    } 
} 
+0

Ils provoquent les éléments suivants: bundle.js: 53 Promesse {_d: Object} bundle.js: 125 ReferenceError: l'erreur n'est pas définie (...) mais ne répond pas avec les informations récupérées. –

+0

Comme indiqué dans les commentaires, '.catch (erreur);' est un problème. – Bergi