Javascript ES6承诺功能选项

时间:2017-08-17 07:40:30

标签: javascript es6-promise

我有一个异步函数,可以调用多个其他异步函数之一;我可以把它分成多个.then.catch链,但我想知道我是否可以将它们合并在一起。 我所拥有的是:

return new Promise((resolve, reject) => {
    if (payload.method === 'a')
        doFunctionA(arg)
        .then((x) => resolve())
        .catch((error) => reject())
    else if (payload.method === 'b')
        doFunctionB()
        .then((x) => resolve())
        .catch((error) => reject())
});

这是简化的,但有没有另一种方法来写这个,以便then和catch只写一次?像:

return new Promise((resolve, reject) => {
    var f = (payload.method === 'a' ? doFunctionA(arg) : doFunctionB());
    f()
    .then((x) => resolve())
    .catch((error) => reject())
});

2 个答案:

答案 0 :(得分:0)

由于private static List<Earthquake> extractFeatureFromQuakeml(String earthquakeXML) throws XmlPullParserException, IOException { XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance(); xmlFactoryObject.setNamespaceAware(true); XmlPullParser myparser = xmlFactoryObject.newPullParser(); myparser.setInput(new StringReader(earthquakeXML)); // If the JSON string is empty or null, then return early. if (TextUtils.isEmpty(earthquakeXML)) { return null; } // Create an empty ArrayList that we can start adding earthquakes to List<Earthquake> earthquakes = new ArrayList<>(); try { int eventType = myparser.getEventType(); Log.d("Step", "Started parsing"); Earthquake earthquake = null; while (eventType != XmlPullParser.END_DOCUMENT) { // String location = ""; // String time = ""; // double magnitude = 0.0; // String url = ""; String tag = myparser.getName(); String text = ""; switch (eventType) { case XmlPullParser.START_TAG: //Log.d("Step","Started parsing, end tag: " + tag); if (tag.equalsIgnoreCase("event")) { url = myparser.getAttributeValue(null, "publicID"); Log.d(LOG_TAG, "Url" + url); myparser.next(); // We are creating Earthquake here earthquake = new Earthquake(); earthquake.setUrl(url); } break; case XmlPullParser.END_TAG: text = myparser.getText(); if (text != null) { if (tag.equalsIgnoreCase("type") && text.equalsIgnoreCase("region name")) { myparser.nextText(); earthquake.setLocation(myparser.getText()); Log.d(LOG_TAG, "Location" + earthquake.getLocation()); } } if (tag.equalsIgnoreCase("creationTime")) { earthquake.setTime(myparser.getText()); // time = "" + myparser.getText(); Log.d(LOG_TAG, "Time" + earthquake.getTime()); } if (tag.equalsIgnoreCase("mag")) { myparser.next(); myparser.next(); earthquake.setMagnitude(myparser.getText()); // String smagnitude = myparser.getText(); Log.d(LOG_TAG, "Magnitude" + earthquake.getMagnitude()); } if (tag.equalsIgnoreCase("event")) { earthquakes.add(earthquake); } break; } eventType = myparser.next(); } } catch (XmlPullParserException e) { Log.e("QueryUtils", "Problem parsing the earthquake XML results", e); } // Return the list of earthquakes return earthquakes; } doFunctionA已经返回doFunctionB,您不必将它们包装在另一个Promises中:

Promise

答案 1 :(得分:0)

是。除非你两次致电f,否则你的方式非常接近。尝试类似:

return new Promise((resolve, reject) => {
    (payload.method === 'a' ? doFunctionA : doFunctionB)(arg)
        .then((x) => resolve())
        .catch((error) => reject())
    ;
});

更好的方法是按照@Patrick

的建议返回该功能
return (payload.method === 'a' ? doFunctionA : doFunctionB)(arg);