我对这个Promise的东西感到疯狂

时间:2017-11-23 22:19:00

标签: promise

希望有人可以提供帮助。 我正在制作魔镜中的翻译部分。我使用天气模块的内部node_helper.js:让我向你展示代码:

            if (this.config.lang != 'en'){
                console.log("in Translate");
                    translate(alerts.description, {to: this.config.lang}).then((res) => {console.log(res.text); return res;});
                    translate(alerts.expires, {to: this.config.lang}).then(res => {console.log(res.text)});
                translate(alerts.message, {to: this.config.lang}).then(res => {console.log(res.text)});
            }

等等。如果我查看config.log,我会得到我喜欢看到的所有结果,但我无法在下面的代码中使用它们,,,请帮助

1 个答案:

答案 0 :(得分:0)

您可以使用using System; using System.Xml; using System.Xml.Serialization; using System.IO; using System.Windows.Forms; namespace test { public class SerializeObjectTest { public static void Test() { var a1 = new A(); a1.init(); SerializeObject<A>(a1, "d:\\1.xml"); var a2 = DeSerializeObject<A>("d:\\1.xml"); a1.b1.x = 5; // this will change also the value of a1.b2.x a2.b1.x = 5; // this will not!!!!! change also the value of a2.b2.x MessageBox.Show( "a1.b1.x==a1.b2.x : " + a1.b1.x + "?=" + a1.b2.x + "\r\n" + "a2.b1.1==a2.b2.x : " + a2.b1.x + "?=" + a2.b2.x + " !!\r\n", "Save.SaveAble" ); } public class A { public void init() { b1 = new B() { x = 100 }; b2 = b1; } public B b1; public B b2; } public class B { public double x; } /// <summary> /// Serializes an object. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="serializableObject"></param> /// <param name="fileName"></param> public static void SerializeObject<T>(T serializableObject, string fileName) { if (serializableObject == null) { return; } try { XmlDocument xmlDocument = new XmlDocument(); XmlSerializer serializer = new XmlSerializer(serializableObject.GetType()); using (MemoryStream stream = new MemoryStream()) { serializer.Serialize(stream, serializableObject); stream.Position = 0; xmlDocument.Load(stream); xmlDocument.Save(fileName); stream.Close(); } } catch (Exception ex) { //Log exception here } } /// <summary> /// Deserializes an xml file into an object list /// </summary> /// <typeparam name="T"></typeparam> /// <param name="fileName"></param> /// <returns></returns> public static T DeSerializeObject<T>(string fileName) { if (string.IsNullOrEmpty(fileName)) { return default(T); } T objectOut = default(T); try { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(fileName); string xmlString = xmlDocument.OuterXml; using (StringReader read = new StringReader(xmlString)) { Type outType = typeof(T); XmlSerializer serializer = new XmlSerializer(outType); using (XmlReader reader = new XmlTextReader(read)) { objectOut = (T)serializer.Deserialize(reader); reader.Close(); } read.Close(); } } catch (Exception ex) { //Log exception here } return objectOut; } } } 协调完成所有返回承诺的三个操作,然后在结果Promise.all()回调中使用这些结果:

.then()