2

I have this annoying warning and I'm not able to get rid of it.
Let's say that I have a promise that sends a command to a bluetooth le device, to do a procedure I need to send these commands synchronously.
So what you can do in PromiseKit 6 is create a for loop and append each command using a then to the previous promise. The issue with that approach is that the compiler trigger a warning about an Result of call to 'then(on:flags:_:)' is unused, is like is missing the catch. I'm aware that I can use a cauterize() , but in this case I guess I will lost errors thrown while executing each promise. A when is not viable because it will execute concurrently while I need synchronously.
Here a small sample used with strings, in my real application they are NOT string.

 static func promise_startProcedure(with commands: [String]) -> Promise<Void> {
        let promise = Promise()
        for command in commands {
            promise.then { //<-- WARNING SHOW HERE
                Promise.value(command)
            }
        }
        return promise
    }

Is there a way to get rid of this warning?
[UPDATE]
I did a mistake, the answer was not correct bu pointed me to the right direction.

static func promise_startProcedure(with commands: [String]) -> Promise<Void> {
            var promise = Promise()
            for command in commands {
                promise = promise.then { 
                    Promise.value(command)
                }
            }
            return promise
        }
Andrea
  • 26,120
  • 10
  • 85
  • 131

2 Answers2

3

Try this. I just added Underscore to suppress the warning and did not get that warning.

static func promise_startProcedure(with commands: [String]) -> Promise<Void> {
    let promise = Promise()
    for command in commands {
        _ = promise.then {
            Promise.value(command)
        }
    }
    return promise
}
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
Anand
  • 1,820
  • 2
  • 18
  • 25
1

You can add @discardableResult to the declaration of the method. From the docs:

For example, the discardableResult attribute on a function declaration indicates that, although the function returns a value, the compiler shouldn’t generate a warning if the return value is unused.

J. Doe
  • 12,159
  • 9
  • 60
  • 114
  • It won't work, the warning is shown here `promise.then {` but thanks for pointing it, I will edit the question – Andrea Apr 29 '19 at 11:43