using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Interview.libs { /// <summary> /// Promise状态 /// </summary> enum PromiseStatus { /// <summary> /// 准备好 /// </summary> Pending = 0, /// <summary> /// 完成 /// </summary> Fufilled = 1, /// <summary> /// 失败 /// </summary> Faill = 2 } /// <summary> /// 模拟JS的Promise /// </summary> public class Promise { public Promise(Action<Action<object>, Action<Exception>> init) { init(_Resolve, _Reject); } public static Promise Resolve(object data) { return new Promise((resolve, reject) => { new Thread(() => { Thread.Sleep(10); resolve(data); }).Start(); }); } public static Promise Resolve(Promise promise) { return new Promise((resolve, reject) => { promise.Then(res => { resolve(res); return res; }); }); } /// <summary> /// 当前promise的状态 /// </summary> private PromiseStatus Status = PromiseStatus.Pending; /// <summary> /// 当前promise的数据 Object / Promise /// </summary> private object Data = default(object); /// <summary> /// 当前promise的错误 /// </summary> private Exception Error = null; /// <summary> /// 当前promise 完成的处理韩式 /// </summary> private List<Func<object, object>> ResolveList = new List<Func<object, object>>(); /// <summary> /// 当前promise 失败的处理函数 /// </summary> private List<Func<Exception, Exception>> RejectList = new List<Func<Exception, Exception>>(); /// <summary> /// 成功的处理 /// </summary> /// <param name="data"></param> private void _Resolve(object data) { Status = PromiseStatus.Fufilled; Data = data; ResolveList.ForEach(fn => { Data = fn(Data); }); } /// <summary> /// 失败的处理 /// </summary> /// <param name="error"></param> private void _Reject(Exception error) { Status = PromiseStatus.Faill; Error = error; RejectList.ForEach(fn => { Error = fn(Error); }); } /// <summary> /// 链式调用的处理函数 /// </summary> /// <param name="resole">成功处理</param> /// <param name="reject">失败处理</param> /// <returns></returns> public Promise Then(Func<object, object> resole, Func<Exception, Exception> reject = null) { if (resole != null) { if (Status == PromiseStatus.Fufilled) { Data = Promise.Resolve(Data).Then(resole); } else { ResolveList.Add(resole); } } if (reject != null) { if (Status == PromiseStatus.Fufilled) { Error = reject(Error); } else { RejectList.Add(reject); } } return this; } } }
这个仅仅实现Promise基础部分,使用如下
static void PromiseFn() { var promise = new Promise((resolve, reject) => { Console.WriteLine("Promise: init"); new Thread(() => { Thread.Sleep(5000); Console.WriteLine("Promise: start"); resolve(1); }).Start(); }); promise.Then(res => { Thread.Sleep(5000); Console.WriteLine("Promise: then1"); return res + "2"; }).Then(res => { Thread.Sleep(5000); Console.WriteLine("Promise: then2"); return Promise.Resolve(res + "*"); }).Then(res => { Thread.Sleep(5000); Console.WriteLine("Promise: then3"); return res + "10"; }).Then(res => { Thread.Sleep(5000); Console.WriteLine("Promise: end:" + res); return res; }); }
标签:return,Thread,C#,res,Promise,new,promise From: https://www.cnblogs.com/ehuanrum/p/17538942.html