Dart 里常见的 Completer

385 阅读1分钟

可以把  回调方法转为 Future

Completer _completer = new Completer();	
Future<T> doOperation() {
    _startOperation();		
    return _completer.future; 
    // Send future object back to client.	 
}	

// Something calls this when the value is ready.	 
void _finishOperation(T result) {		
    _completer.complete(result);	
}	 

// If something goes wrong, call this.	
void _errorHappened(error) {		
    _completer.completeError(error);	
}