Search code examples
swiftswift4promisekit

Cannot convert value of type 'Promise<[NSDictionary]>' to expected argument type 'Guarantee<Void>'


Currently, I'm having issues when I updated my PromiseKit from Swift 2 to Swift 4. A lot of my Promises are returning this issue from NSDictionary to Guarantee Void. I don't understand why this issue arises. If someone could explain it to me, that'd be much appreciated!

func getPlayerGameLog(sportName: String) -> Promise<NSArray> {
    let team = (self as? PlayerWithPositionAndGame)?.team
    let position = (self as? PlayerWithPositionAndGame)?.position

    if sportName == "mlb" {
        return when(Data.mlbPlayerGameLogs[id].get(), Data.sportsTeams[sportName].get()).then { (result, teams) -> NSArray in
            if result.count == 0 {
                return []
            } else {
                // Omitted Code
                return gameStats
            }
        }
    } else if sportName == "nfl" {
        return when(Data.nflPlayerGameLogs[id].get(), Data.sportsTeams[sportName].get()).then { (result, teams) -> NSArray in
            if result.count == 0 {
                return []
            } else {
                // Omitted Code
                return gameStats
            }
        }
    } else {
        return Data.nbaPlayerGameLogs[id].get().then { gameLogs in
            return []
        }
    }
}

Now the declaration of game logs is as follows

static let mlbPlayerGameLogs = MultiCache { id in API.getMLBPlayerGameLogs(playerID: id) }

It's the same for mlb/nfl/nba.

The purpose of the function is pretty simple. It's just pulling in the team info into the function and returning it.

Errors occur on the "return when()" Lines


Solution

  • First of all, it's swift 4 and you shouldn't use NS... types, there are pure swift types right now.

    Next thing, according to the release notes:

    • then is fed the previous promise value and requires you return a promise.
    • done is fed the previous promise value and returns a Void promise (which is 80% of chain usage)
    • map is fed the previous promise value and requires you return a non-promise, ie. a value.

    almost all then should be replaced with map.

    Also, you should rewrite when as when(fulfilled:) to return Promise, not Guarantee.