<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def waitForGreenlet(g):
 """Link greenlet completion to Deferred"""

    from twisted.internet import reactor
    assert reactor.greenlet == getcurrent(), "must invoke this in the reactor greenlet"
    d = defer.Deferred()

    def cb(g):
        try:
            d.callback(g.get())
        except:
            d.errback(failure.Failure())

    g.link(d)
    return d