2015-01-15 13 views
9

मैं superagent के साथ es6 वादों का उपयोग करने का प्रयास कर रहा हूँ। मैं ऐसे फ़ंक्शन को कॉल करने का प्रयास कर रहा हूं जिसमें एक शानदार अनुरोध है जिसमें अंदर लपेटा गया है।वादा करता है es6 और superagent

Request.post(buildReq).then(res => { 
if (res.ok) {//process res} 
}); 

यहाँ समारोह रैपिंग superagent

static post(params) { 
    superagent 
     .post(params.url) 
     .send(params.payload) 
     .set('Accept', 'application/json') 
     .end((error, res) => { 
     return this.Promise.resolve(res); 
     }) 
     .bind(this); 
    } 

जब मैं

static post(params) { 
    return Promise.resolve(superagent 
     .post(params.url) 
     .auth(params.auth.username, params.auth.password) 
     .send(params.payload) 
     .set('Accept', 'application/json') 
     .end((error, res) => { 
     return this.Promise.resolve(res); 
     }) 
    ); 
    } 
को समारोह की वापसी को बदलने मैं एक त्रुटि

enter code here Uncaught TypeError: Cannot read property 'then' of undefined

हो रही है

ऐसा लगता है कि डेटा मेरे ब्राउज़र के देव उपकरण में वापस आ गया है, लेकिन मैं इसे .then फ़ंक्शन के भीतर नहीं ले सकता। मैं वादे से प्रतिक्रिया कैसे प्राप्त कर सकता हूं।

उत्तर

25

इससे कोई फर्क नहीं पड़ता कि आप end विधि कॉलबैक से क्या लौट रहे हैं, क्योंकि जब आप प्रतिक्रिया प्राप्त करते हैं तो असीमित रूप से निष्पादित किया जाता है और कॉलबैक निष्पादन का परिणाम कहीं भी उपयोग नहीं किया जाता है। स्रोत कोड में here और here देखें। end विधि this देता है, इसलिए आपके दूसरे उदाहरण में आप superagent को प्रतिक्रिया नहीं दे रहे हैं।

static post(params) { 
    return new Promise((resolve, reject) => { 
     superagent 
      .post(params.url) 
      .auth(params.auth.username, params.auth.password) 
      .send(params.payload) 
      .set('Accept', 'application/json') 
      .end((error, res) => { 
       error ? reject(error) : resolve(res); 
      }); 
    }); 
} 
+0

भयानक समाधान का एक बहुत के लिए इसकी आवश्यकता मामले में एक और अधिक consise संस्करण है,। – Sinux

4

कभी-कभी आप new Promise(...) की वजह से तो आप सीधे Promise.reject और Promise.resolve उपयोग कर सकते हैं एक खरोज स्तर से बचने के लिए करना चाहते हैं: अपने post विधि चाहिए प्रतिक्रिया प्राप्त करने के लिए की तरह लग रहा है।

static post(params) { 
    return superagent 
      .post(params.url) 
      .auth(params.auth.username, params.auth.password) 
      .send(params.payload) 
      .set('Accept', 'application/json') 
      .end((error, res) => { 
       return error ? Promise.reject(error) : Promise.resolve(res); 
      }); 
    }); 
} 
0

यह आपको अनुरोध

import request from "superagent"; 

const withPromiseCallback = (resolve, reject) => (error, response) => { 
    if (error) { 
    reject({error}); 
    } else { 
    resolve(response.body); 
    } 
}; 

export const fetchSuggestions = (search) => new Promise((resolve, reject) => 
request. 
    get("/api/auth/get-companies/0/50"). 
    type("form"). 
    set("Accept", "application/json"). 
    query({ 
     search, 
    }). 
    end(withPromiseCallback(resolve, reject)) 
); 

export const fetchInitialInformation =() => new Promise((resolve, reject) => 
    request. 
    get("/api/auth/check"). 
    set("Accept", "application/json"). 
    end(withPromiseCallback(resolve, reject)) 
); 
संबंधित मुद्दे