0

I have a timer set for one minute and an asynchronous task already initiated (which will return a true or false). On timer expire,

  1. if the asynchronous task has returned and if it is true I want to show 'Success'.

  2. If the asynchronous task has returned and the value is false, I want to display 'Failure'.

  3. If asynchronous task is not returned yet,I want to wait for the response and show 'Success' and 'Failure' according to the async tasks value.

Imperative Programming (Pseudo code):

//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.
}

Functional Programming: ?????

Sunil
  • 3,404
  • 10
  • 23
  • 31
Ambareesh B
  • 499
  • 1
  • 5
  • 14
  • Is this a question about functional programming, or about some particular async library? What makes you think your proposed solution isn't "functional?" Maybe it'd help if you had a target language in mind? – Fried Brice Jan 23 '18 at 06:06
  • Actually I thought to write a pseudo code. It is fact that the cose is a little biased toward Android. – Ambareesh B Jan 23 '18 at 06:50
  • Your solution doesn't look particularly un-functional to me, so I don't know if I totally understand your question. – Fried Brice Jan 23 '18 at 07:05
  • I use a global variable to store the state. The state keep track of asynchronous tasks. when the second function returns it checks the state to do next action? So not a functional way right? – Ambareesh B Jan 25 '18 at 02:31
  • So you want to avoid reassignment? You won't be able to, but you could use a framework that hides it from you (e.g. Akka), or you could encapsulate it behind an interface by using specialized classes (e.g. Futures/Promises). – Fried Brice Jan 25 '18 at 04:50
  • Yes I understand Thank you for all. – Ambareesh B Jan 27 '18 at 06:54

0 Answers0