异步函数编程

时间:2018-01-22 01:50:50

标签: functional-programming purely-functional

我有一个定时器设置为一分钟,并且已启动异步任务(将返回true或false)。在计时器到期时,

  1. 如果异步任务已经返回,如果是,我想显示'成功'

  2. 如果异步任务已返回且值为false,我想显示' Failure'

  3. 如果还没有返回异步任务,我想等待响应并显示“成功”#39;和'失败'根据异步任务值。

  4. 命令式编程(伪代码):

    //Use a global variable 
    var asyncValue = null/nil //To hold the value of async tasks output.
    
    //Asynchronous call back.
    
    onAsyncReturned(bool value){
    ayncValue = value
    }
    
    //So finally, on timer expire.
    
    switch(asyncValue){
    
    case null/nil : //Asynchronous task not returned yet.
    
    case true     : //Show 'Success' message.
    
    case false    : //Show 'Failure' message.
    }
    

    功能编程:?????

0 个答案:

没有答案